python正则表达式匹配ip地址

首先要引入re模块

import re

re.search(r'(([01]{0,1}\d{0,1}\d|2[0-4]\d|25[0-5])\.){3}([01]{0,1}\d{0,1}\d|2[0-4]\d|25[0-5])','1.2.3.4')

真实实战

import urllib.request        
import re
url="http://www.66ip.cn/"    
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'}      #模拟浏览器访问,我用的是谷歌浏览器。
req=urllib.request.Request(url=url,headers=headers)
response=urllib.request.urlopen(req)
html=response.read().decode('gb2312')  #解码,我要爬的网站编码是gb2312
p=r'(?:(?:[01]{0,1}\d{0,1}\d|2[0-4]\d|25[0-5])\.){3}(?:[01]{0,1}\d{0,1}\d|2[0-4]\d|25[0-5])' 
iplist=re.findall(p,html)
for i in iplist:
    print(i)

猜你喜欢

转载自blog.csdn.net/qqaazzww1234/article/details/80286359