爬虫-爬取代理ip网页里的ip

时间:2021-08-20 17:02:43

思想:先打开url,获取其html然后下载出网页中符合正则表达式的ip

import urllib.request
import re

def open_url(url):
req=urllib.request.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36')
page=urllib.request.urlopen(req)
html=page.read().decode('utf-8')
return html


def get_ip(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])'
#p=r'<img class="BDE_Image" pic_type="0" width="500" height="375" src="[^"]+\.jpg"'
iplist=re.findall(p,html)
for each in iplist:
print(each)


if __name__=='__main__':
url="http://www.proxy360.cn/default.aspx"
html=open_url(url)
get_ip(html)