python网络爬虫——正则获取ip
python网络爬虫,几行代码就可以搞定
1.请求网页,获取数据
#请求网络
def open_url(url):
req = urllib.request.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0')
page = urllib.request.urlopen(req)
html = page.read().decode('utf-8')
return html
2.正则匹配Ip值 ,匹配规则为 (?:(?:[0-1]?\d?\d|2[0-4]\d|25[0-5])\.){3}(?:(?:[0,1]?\d?\d|2[0-4]\d|25[0-5]))
#正则匹配ip值(?:(?:[0-1]?\d?\d|2[0-4]\d|25[0-5])\.){3}(?:(?:[0,1]?\d?\d|2[0-4]\d|25[0-5]))
def get_iplist(html):
p = r'(?:(?:[0-1]?\d?\d|2[0-4]\d|25[0-5])\.){3}(?:(?:[0,1]?\d?\d|2[0-4]\d|25[0-5]))'
iplist = re.findall(p,html)
for each in iplist:
print(each)
3.全部代码,打印输出
import urllib.request
import re
#请求网络
def open_url(url):
req = urllib.request.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0')
page = urllib.request.urlopen(req)
html = page.read().decode('utf-8')
return html
#正则匹配ip值(?:(?:[0-1]?\d?\d|2[0-4]\d|25[0-5])\.){3}(?:(?:[0,1]?\d?\d|2[0-4]\d|25[0-5]))
def get_iplist(html):
p = r'(?:(?:[0-1]?\d?\d|2[0-4]\d|25[0-5])\.){3}(?:(?:[0,1]?\d?\d|2[0-4]\d|25[0-5]))'
iplist = re.findall(p,html)
for each in iplist:
print(each)
if __name__ == '__main__':
url = 'http://www.xicidaili.com/'
get_iplist(open_url(url))
输出结果:
python刚入门,有点意思。
有兴趣一起讨论,欢迎加入我的Java与Android逆向开发交流QQ群,一起学习编码。