找个代理网站把代理ip爬下来做代理池

本文内容仅供学习交流使用,不具有任何商业用途,如有问题请即时联系我处理。--Python逐梦者。

某度上很多免费代理的网站,今天尝试来爬一个试着做下代理池。

代码开始:

 1 """
 2     找一个免费代理,然后将它搭建成爬虫的代理池
 3 """
 4 import requests
 5 import csv
 6 import time
 7 import parsel
 8 import random
 9 
10 # f = open('89免费代理.csv', mode='a', encoding='utf-8-sig', newline='')
11 # csvWriter = csv.DictWriter(f, fieldnames=[
12 #     'ip地址',
13 #     '端口',
14 #     '位置',
15 #     'isp服务商',
16 # ])
17 # csvWriter.writeheader() # 将头写入
18 
19 headers = {
20     "cookie": "Hm_lvt_f9e56acddd5155c92b9b5499ff966848=1636356943; Hm_lpvt_f9e56acddd5155c92b9b5499ff966848=1636356954",
21     "referer": "https://www.xxip.cn/",
22     "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36",
23 }
24 
25 ip_list = []
26 port_list = []
27 
28 # 多页爬取
29 for page in range(1, 51): # 爬五十页
30     time.sleep(random.randint(2, 10))
31     print("==========开始爬取第{}页==========".format(page))
32     # 定义url
33     url = f'https://www.xxip.cn/index_{page}.html'
34     # 开始请求
35     response = requests.get(url=url, headers=headers)
36     if response.status_code == 200:
37         print("数据获取成功,开始解析!")
38     # 选择器,要提取ip和端口以供检查使用
39     selector = parsel.Selector(response.text)
40     # 选取ip列表
41     lis = selector.css('.layui-table tbody tr')
42     # 读取列表
43     for li in lis:
44         ip = li.css('td:nth-child(1)::text').get().strip() # ip地址
45         port = li.css('td:nth-child(2)::text').get().strip() # 端口
46         location = li.css('td:nth-child(3)::text').get().strip() # 位置
47         isp = li.css('td:nth-child(4)::text').get().strip() # 运营商
48         ip_list.append(ip) # 测试用
49         port_list.append(port) # 测试用
50         # print(ip, port, location, isp, sep='|')
51         # # 写入到csv文档
52         # dit = {
53         #     'ip地址':ip,
54         #     '端口':port,
55         #     '位置':location,
56         #     'isp服务商':isp,
57         # }
58         # csvWriter.writerow(dit) # 逐行写入到csv文档
59 
60     # print(ip_list, port_list)
61     available_ips = [] # 可用ip
62     # 开始测试第page页的ip
63     print("=====开始测试{}页的ip。=====".format(page))
64     for ip, port in zip(ip_list,port_list):
65         # 开始组建代理
66         proxies = {
67             'http':ip + ":" + port
68         }
69         # print(proxies) 打印看是否符合预期
70         try:
71             res = requests.get('https://www.baidu.com', proxies=proxies, timeout=5)
72             if res.status_code == 200: # 状态码为200则为响应成功
73                 print(ip + "可用!")
74                 available_ips.append(ip)
75             else:
76                 print(ip + "无响应!")
77         except:
78             pass
79 
80         # 接下来保存可用ip
81         with open('89免费代理.csv', mode='a', encoding='utf-8-sig', newline='') as f:
82             csvWriter = csv.writer(f) # 写入器
83             csvWriter.writerow([ip, port]) # 将ip和端口进行保存
84         print("====第{}页的的可用ip保存成功!====".format(page))

爬完部分截图如下:

 逐梦很累,坚持加油。--一叶孤城。

原文地址:https://www.cnblogs.com/mafu/p/15525128.html