当前位置:Gxlcms > Python > python匹配url中是否存在IP地址的方法

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

时间:2021-07-01 10:21:17 帮助过:32人阅读

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

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

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. '''
  4. 功能:对于给定的URL,检测其中是否包含IP
  5. '''
  6. import re
  7. def ip_exist_two(one_url):
  8. compile_rule = re.compile(r'(?<![\.\d])(?:\d{1,3}\.){3}\d{1,3}(?![\.\d])')
  9. match_list = re.findall(compile_rule, one_url)
  10. if match_list:
  11. print match_list
  12. else:
  13. print 'missing................'
  14. def ip_exist_one(one_url):
  15. compile_rule = re.compile(r'\d+[\.]\d+[\.]\d+[\.]\d+')
  16. match_list = re.findall(compile_rule, one_url)
  17. if match_list:
  18. print match_list
  19. else:
  20. print 'missing................'
  21. if __name__ == '__main__':
  22. ip_list = ['http://101.23.45.67/sd/sd.html','http://www.baidu.com',
  23. 'http://34.54.65.3/dsdfjkk.htm','http://dhj.fdjjd.com/78078979/dsdfjkk.htm']
  24. for one_url in ip_list:
  25. ip_exist_one(one_url)
  26. print '****************************************************'
  27. for one_url in ip_list:
  28. ip_exist_two(one_url)

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

下面是打印出来的结果

  1. ['101.23.45.67']
  2. missing................
  3. ['34.54.65.3']
  4. missing................
  5. ****************************************************
  6. ['101.23.45.67']
  7. missing................
  8. ['34.54.65.3']
  9. missing................

相关推荐:

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

以上就是python 匹配url中是否存在IP地址的方法的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行