python 匹配url中是否存在IP地址的方法


本文摘自php中文网,作者不言,侵删。

这篇文章主要介绍了关于python 匹配url中是否存在IP地址的方法,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

因为需要检测一个一个链接中是否包含了IP地址,在这里需要使用到正则表达式 ,python完美的支持了正则表达式,在这里使用re模块来完成,对正则表达式并不是很熟练,每次都是需要用的时候现查一下然后写一下,这里给出来自己的代码以及借鉴别人的匹配模式

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

#!/usr/bin/env python

# -*- coding: utf-8 -*-

'''

功能:对于给定的URL,检测其中是否包含IP

'''

import re

def ip_exist_two(one_url):

    compile_rule = re.compile(r'(?<![\.\d])(?:\d{1,3}\.){3}\d{1,3}(?![\.\d])')

    match_list = re.findall(compile_rule, one_url)

    if match_list:

        print match_list

    else:

        print 'missing................'

def ip_exist_one(one_url):

    compile_rule = re.compile(r'\d+[\.]\d+[\.]\d+[\.]\d+')

    match_list = re.findall(compile_rule, one_url)

    if match_list:

        print match_list

    else:

        print 'missing................'

if __name__ == '__main__':

    ip_list = ['http://101.23.45.67/sd/sd.html','http://www.baidu.com',

    'http://34.54.65.3/dsdfjkk.htm','http://dhj.fdjjd.com/78078979/dsdfjkk.htm']

    for one_url in ip_list:

        ip_exist_one(one_url)

    print '****************************************************'

    for one_url in ip_list:

        ip_exist_two(one_url)

ip_exist_one(one_url)里面是自己的匹配模式,个人感觉更贱练一下,ip_exist_two(one_url)里面是网上提供的匹配IP的正则表达式,感觉比较繁杂一下,不过试验了一下都是可以正确匹配出来结果的。

下面是打印出来的结果

1

2

3

4

5

6

7

8

9

['101.23.45.67']

missing................

['34.54.65.3']

missing................

****************************************************

['101.23.45.67']

missing................

['34.54.65.3']

missing................

相关推荐:

Django项目中包含多个应用时对url的配置方法

以上就是python 匹配url中是否存在IP地址的方法的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python实现一个搜索引擎(pylucene)实例教程

Python如何使用urllib/urllib2访问http的get及post详解

Python中关于django使用的图文详解

Python怎么去除html标签

Python编写一个三级while的循环菜单实例

Python中关于日期加减法的操作详解

二进制数1001001转换成十进制数等于多少

Python中list函数怎么用

Python3终端按哪里跳出循环

零基础的小白怎么学Python

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...