爬取代理IP

 1 #coding=utf-8
 2 import re
 3 import urllib.request
 4 import urllib.parse
 5 import os
 6 from urllib.error import URLError,HTTPError
 7 url='http://www.kuaidaili.com/ops/' #http://www.xicidaili.com/  http://www.proxy360.cn/default.aspx  http://www.kuaidaili.com/ops/
 8 headers={'Accept':'text/html,application/xhtml+xm…plication/xml;q=0.9,*/*;q=0.8',
 9          'Accept-Encoding':'gzip,deflate',
10          'Accept-Language':'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
11          'Host':'fs.xicidaili.com',
12          'Cookie':'Hm_lvt_0cf76c77469e965d2957f0553e6ecf59=1511441689,1511784134,1511785760; Hm_lpvt_0cf76c77469e965d2957f0553e6ecf59=1511785760',
13          'Connection':'keep-alive',
14          'User-Agent':'Mozilla/5.0 (Windows NT 6.1; W…) Gecko/20100101 Firefox/57.0'
15 }
16 headers=urllib.parse.urlencode(headers).encode('utf8')
17 rep=urllib.request.Request(url,headers)
18 reponse=urllib.request.urlopen(rep)
19 html=reponse.read().decode(encoding='UTF-8')
20 reg=r'\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b'
21 ip_list=re.findall(reg,html)
22 print(ip_list)
23 f=open('ip.txt','a')
24 f.write(str(ip_list)+'\n')
25 f.close()


 

posted @ 2017-11-30 15:56  bro  阅读(540)  评论(0编辑  收藏  举报