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

作者:Together_CZ 时间:2023-04-13 14:29:08 

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


#!/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的正则表达式,感觉比较繁杂一下,不过试验了一下都是可以正确匹配出来结果的。

下面是打印出来的结果


['101.23.45.67']
missing................
['34.54.65.3']
missing................
****************************************************
['101.23.45.67']
missing................
['34.54.65.3']
missing................

来源:https://blog.csdn.net/Together_CZ/article/details/55508275

标签:python,匹配,url,IP
0
投稿

猜你喜欢

  • python Pygame的具体使用讲解

    2021-01-15 21:41:26
  • go语言 bool类型的使用操作

    2024-04-26 17:35:03
  • python 队列基本定义与使用方法【初始化、赋值、判断等】

    2023-08-13 06:34:46
  • python logging模块的分文件存放详析

    2023-04-02 20:27:32
  • 解决Linux系统中python matplotlib画图的中文显示问题

    2023-12-18 22:10:53
  • Kettle连接Oracle数据库方法((Oracle19c&Oracle11g))

    2024-01-22 04:59:56
  • Python Pygame实现可控制的烟花游戏

    2021-09-08 20:29:02
  • Django框架视图介绍与使用详解

    2021-03-29 08:50:46
  • Python将多个excel文件合并为一个文件

    2021-10-16 14:06:55
  • PHP session有效期问题

    2023-11-23 20:49:10
  • 基于python的Paxos算法实现

    2023-04-15 07:40:58
  • JavaScript table的排序类

    2008-10-06 12:56:00
  • 详解Python装饰器的四种定义形式

    2022-06-20 13:35:04
  • python无限生成不重复(字母,数字,字符)组合的方法

    2021-02-15 14:08:49
  • 5款实用的python 工具推荐

    2021-08-09 20:13:28
  • PHP5 mysqli的prepare准备语句使用说明

    2023-11-22 12:50:29
  • Oracle数据库中SQL语句的优化技巧

    2024-01-27 01:33:27
  • 在python tkinter界面中添加按钮的实例

    2023-06-03 08:30:41
  • 基于Python的Houdini插件开发过程详情

    2023-11-28 17:28:02
  • 完美解决Python matplotlib绘图时汉字显示不正常的问题

    2023-09-28 05:30:55
  • asp之家 网络编程 m.aspxhome.com