昨天制作了ip地址池,当时检测ip是否可用的时候用的方法是判断requests里的状态码,如果状态码是200,那么请求成功,否则被视为无用的代理。

今天发现这些被筛选出来的代理,在requests里是能用的,但是在chromedriver的headless模式下无法使用。附图

如上图,同样的ip和端口,requests里可以用(状态码200),但是selenium不能用。原因还没找出来,所以打算用http://httpbin.org/ip来做代理ip是否可用的判断。

新的代理池代码如下:

 1 from bs4 import BeautifulSoup
 2 import re,time
 3 from selenium import webdriver
 4 from selenium.common.exceptions import TimeoutException
 5 def estimate_time(ip_test_date,ip_rest_time):#rest_time+test_date-current_time
 6     new_ip_test_date=\'20\'+ip_test_date+\':00\'
 7     time_stamp = time.mktime(time.strptime(new_ip_test_date, \'%Y-%m-%d %H:%M:%S\'))
 8     d_ip_rest_time=re.findall(\'\d\',ip_rest_time)
 9     a_ip_rest_time=re.findall(\'\D\',ip_rest_time)
10     if a_ip_rest_time[0]==\'分钟\':
11         da_ip_rest_time=int(d_ip_rest_time[0])*60
12     elif a_ip_rest_time[0]==\'小时\':
13         da_ip_rest_time = int(d_ip_rest_time[0]) * 3600
14     else:
15         da_ip_rest_time = int(d_ip_rest_time[0]) * 86400
16     result_time=time_stamp+da_ip_rest_time-time.time()
17     return result_time
18 def ip_available(ip_type,ip_add,ip_port):
19     try:
20         chrome_options = webdriver.ChromeOptions()
21         chrome_options.add_argument(\'--headless\')
22         chrome_options.add_argument(\'--disable-gpu\')
23         chrome_options.add_argument(\'--proxy-server=%s://%s:%s\'%(ip_type,ip_add,ip_port))
24         browser = webdriver.Chrome(chrome_options=chrome_options)
25         browser.get("http://httpbin.org/ip")
26         html_result = browser.page_source
27         pattern3=re.compile(\'<head></head><body>.*?"origin": "(.*?)".*?</body></html>\',re.S)
28         final_result=re.findall(pattern3,html_result)
29         if len(final_result)==1:
30             final_result=final_result[0]
31             return final_result
32         else:
33             return \'no_list\'
34 
35     except TimeoutException:
36         return \'time_out\'
37 try:
38     chrome_options = webdriver.ChromeOptions()
39     chrome_options.add_argument(\'--headless\')
40     chrome_options.add_argument(\'--disable-gpu\')
41     browser = webdriver.Chrome(chrome_options=chrome_options)
42     browser.get("http://www.xicidaili.com/nn")
43     html=browser.page_source
44     browser.quit()
45 except TimeoutException:
46     print(\'Time Out\')
47 dict_http={}
48 dict_https={}
49 soup=BeautifulSoup(html,\'lxml\')
50 lists=soup.tbody.contents
51 count=0
52 while count<199:
53     count+=2
54     need_jiexi=str(lists[count])
55     pattern=re.compile(\'<td>(.*?)</td>\',re.S)
56     items=re.findall(pattern,need_jiexi)
57     pattern2=re.compile(\'<div class="bar_inner fast" style="width:(.*?)%">\',re.S)
58     items2=re.findall(pattern2,need_jiexi)
59     ip_place_list=re.findall(\'<a href.*?">(.*?)</a>\',items[2])
60     if len(ip_place_list)==1:
61         ip_place = ip_place_list[0]
62     else:
63         continue
64     if len(items2)==2:
65         ip_speed = items2[0]
66         ip_connect_time = items2[1]
67         if int(ip_speed) and int(ip_connect_time) > 79:
68             ip_speed=ip_speed
69             ip_connect_time=ip_connect_time
70         else:
71             continue
72     else:
73         continue
74     ip_rest_time = items[4]
75     ip_test_date = items[5]
76     if estimate_time(ip_test_date,ip_rest_time)>600:
77         ip_test_date=ip_test_date
78         ip_rest_time=ip_rest_time
79     else:
80         continue
81     ip_type = items[3].lower()
82     ip_add = items[0]
83     ip_port = items[1]
84     if ip_available(ip_type,ip_add,ip_port)==ip_add:
85         ip_type=ip_type
86         ip_add=ip_add
87         ip_port=ip_port
88     else:
89         continue
90     if ip_type == \'http\':
91         name = \'ip_address_%d\' % (len(dict_http) + 1)
92         dict_http.update({name: [ip_add, ip_port, ip_place]})
93     else:
94         name = \'ip_address_%d\' % (len(dict_https) + 1)
95         dict_https.update({name: [ip_add, ip_port, ip_place]})
96 print(\'http:\',dict_http)
97 print(\'https:\',dict_https)

代码

1 http: {\'ip_address_1\': [\'218.6.147.232\', \'61202\', \'四川德阳\'], \'ip_address_2\': [\'112.248.23.37\', \'61234\', \'山东枣庄市市中区\'], \'ip_address_3\': [\'180.212.140.199\', \'8118\', \'天津\'], \'ip_address_4\': [\'115.46.65.23\', \'8123\', \'广西南宁\']}
2 https: {}

结果

但是运行这个代码速度有点慢,需要用多线程改进一下。

版权声明:本文为pakhm原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/pakhm/p/8583207.html