python代理爬取存入csv文件

2018-08-17 09:48:20来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

爬取高匿代理

 1 from urllib import request
 2 import re
 3 import time
 4 
 5 f = open('西1.csv','w',encoding='GBK')
 6 headers = {
 7 'Cookie':' _free_proxy_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFVEkiJTJjNTgwNDE4OTYyNTJiNjlmMmU2NDFhZWEwZjExMjFjBjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMVd4THYybCtNcW1yZmJFckpWUDJFdnNzam5hUCtuMGJLNEg0UFUzdE1XWTA9BjsARg%3D%3D--a43f95e415d8ee53f36f90941dbab43b1503d84b',
 8 'User-Agent':' Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36',
 9 }
10 url = 'http://www.xicidaili.com/nn/{}'
11 end = int(input('你想爬几页的代理呢'))
12 for pag in range(1,end+1):
13     fullurl = url.format(pag)
14     res = request.Request(fullurl,headers=headers)
15     response = request.urlopen(res)
16     html = response.read().decode('utf-8')
17     # print(html)
18     list_rule = re.compile(r'<tr.*?</tr>',re.S)
19     list = list_rule.findall(html)
20     # print(list)
21     for td in list:
22         iplist_rule = re.compile(r'<td>(.*?)</td>',re.S)
23         iplist = iplist_rule.findall(td)
24         # print(iplist)
25         for ip_a in iplist:
26             a_rule = re.compile(r'>(.*?)<',re.S)
27             address = a_rule.findall(ip_a)
28             for ip_address in address:
29                 iplist[2]=(ip_address)
30                 f.write(','.join(iplist) + '\n')
31     print('第{}页下载完成'.format(pag))
32     time.sleep(5)
33 f.close()

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Python 爬虫 (五)

下一篇:这是我见过最详细的飞机大战教程!完全零基础小学生都会把!