python代理爬取存入csv文件
程序员文章站
2023-02-20 19:10:39
爬取高匿代理 ......
爬取高匿代理
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()