/
proxypool.py
367 lines (303 loc) · 13.5 KB
/
proxypool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# coding: utf-8
# In[1]:
from urllib.parse import urlencode
import requests
from bs4 import BeautifulSoup
from lxml import etree
import time
import re
from fake_useragent import UserAgent
import sys
import requests
from lxml import etree
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import multiprocessing as mp
# In[32]:
class proxypool:
proxy_html = ['http://free-proxy-list.net',
'http://www.gatherproxy.com/proxylist/country/?c=',
'https://www.us-proxy.org/',
'http://www.proxylisty.com/country/Taiwan-ip-list']
proxy_test = ['http://www.google.com.tw', 'http://www.google.co.jp', 'http://www.yahoo.com', 'http://findbiz.nat.gov.tw/fts/query/QueryBar/queryInit.do']
country_asia1 = ['Taiwan', 'Japan', 'Singapore', 'Korea','Republic%20of%20Korea', 'Hong%20Kong']
country_asia2 = ['Israel', 'China', 'India', 'Iran', 'Pakistan']
country_asia3 = ['Indonesia', 'Thailand', 'Vietnam', 'Philippines', 'Cambodia', 'Malaysia']
country_asia4 = ['Iraq', 'Nepal', 'Mongolia', 'Bangladesh', 'Kazakhstan']
asia = [country_asia1, country_asia2, country_asia3, country_asia4]
country_europe1 = ['Russia', 'France', 'Germany', 'United%20Kingdom', 'Netherlands']
country_europe2 = ['Denmark', 'Poland', 'Ukraine', 'Italy', 'Spain']
country_europe3 = ['Turkey','Slovak%20Republic', 'Bulgaria', 'Czech%20Republic', 'Czechia', 'Hungary']
country_europe4 = ['Romania', 'Serbia', 'Albania', 'Republic%20of%20Moldova', 'Bosnia%20and%20Herzegovina']
europe = [country_europe1, country_europe2, country_europe3, country_europe4]
country_north_america1 = ['United%20States', 'Canada']
north_america = [country_north_america1]
country_south_america1 = ['Brazil', 'Venezuela', 'Colombia', 'Argentina', 'Mexico', 'Ecuador']
country_south_america2 = ['Ecuador', 'Chile', 'Peru', 'Paraguay']
south_america = [country_south_america1, country_south_america2]
country_oceania1 = ['Australia']
oceania = [country_oceania1]
country_africa1 = ['South%20Africa', 'Nigeria', 'Egypt', 'Kenya']
africa = [country_africa1]
country_group1 = country_asia1 + country_europe1 + country_europe2 + country_north_america1 + country_oceania1
country_group2 = country_asia2 + country_europe3 + country_south_america1 + country_africa1
country_group3 = country_asia3 + country_asia4 + country_europe4
group = [country_group1, country_group2, country_group3]
country_world = country_group1 + country_group2 + country_group3
proxy_list = list()
proxy_set = set()
proxy_set_max = 15
#proxy_re = re.compile('[0-9]+(?:\.[0-9]+){3}:\d{2,4}')
def __init__(self, none_freq = 2, path_phantomjs = '/usr/local/Cellar/phantomjs/2.1.1/bin/phantomjs'):
self.session = None
self.response = None
self.path_phantomjs = path_phantomjs
self.none_freq = none_freq
self.none_freq_cnt = 0
self.asia_i = -1
self.europe_i = -1
self.north_america_i = -1
self.south_america_i = -1
self.group_i = -1
self.oceania_i = -1
self.africa_i = -1
def new_session(self):
if self.session is not None:
self.session.close()
self.session = requests.Session()
def reset_proxy(self):
del proxypool.proxy_set
del proxy_list
proxypool.proxy_set = set()
proxypool.proxy_list = list()
def proxy_set_to_proxy_list(list):
proxypool.proxy_list = [{'http':p} for p in proxypool.proxy_set]
def get_proxy1(self):
self.new_session()
self.response = self.session.get(proxypool.proxy_html[0])
selector = etree.HTML(self.response.content)
trs = selector.xpath('//*[@id="proxylisttable"]/tbody/tr')
for tr in trs:
tds = tr.xpath('./td')
ip = tds[0].text
port = tds[1].text
proxypool.proxy_set.add(ip+':'+port)
def get_proxy2(self, country ='Taiwan'):
#PhantomJs_executable_path='/usr/local/Cellar/phantomjs/2.1.1/bin/phantomjs'
def clean_text(text):
import re
if text is None:
text = ''
return text
text = text.encode('latin_1', errors='ignore').decode('utf8', errors='ignore')
text = re.sub(r'[\t\n\r]', r'', text)
return text
# step 1. use PhantomJs to get .js rendered content
browser = webdriver.PhantomJS(executable_path = self.path_phantomjs)
browser.get(proxypool.proxy_html[1]+country)
# step 2. click "Show Full List" button to generate full proxies list
element = WebDriverWait(browser, 1).until(
EC.presence_of_element_located((By.XPATH, '//div[@id="body"]/form/p/input[@type="submit" and @class="button"]')))
element.click()
# step 3. generate selector
selector = etree.HTML(browser.page_source)
# step 4. resolve how many pages
pages = selector.xpath('//div[@id="body"]/form[@id="psbform"]/div[@class="pagenavi"]/a')
# setp 5. resolve trs for first page
trs = selector.xpath('//div[@class="proxy-list"]/table[@id="tblproxy"]/tbody/tr')
for tr in trs[2:]:
tds = tr.xpath('./td')
if len(tds) < 2:
continue
ip = "" if not tds[1].xpath('./text()') else tds[1].xpath('./text()')[0]
port = "" if not tds[2].xpath('./text()') else tds[2].xpath('./text()')[0]
proxypool.proxy_set.add(ip+':'+port)
#print('page 0 done')
# step 6. resolve trs for the rest pages
for i, page in enumerate(pages, 2):
# step 6-1. click nextpage's link
element = WebDriverWait(browser, 1).until(
EC.presence_of_element_located((By.XPATH, '//div[@id="body"]/form[@id="psbform"]/div[@class="pagenavi"]/a[@href="#{}"]'.format(i))))
element.click()
# step 6-2. resolve trs for ith page
selector = etree.HTML(browser.page_source)
trs = selector.xpath('//div[@class="proxy-list"]/table[@id="tblproxy"]/tbody/tr')
for tr in trs[2:]:
tds = tr.xpath('./td')
if len(tds) < 2:
continue
ip = "" if not tds[1].xpath('./text()') else tds[1].xpath('./text()')[0]
port = "" if not tds[2].xpath('./text()') else tds[2].xpath('./text()')[0]
proxypool.proxy_set.add(ip+':'+port)
if len(proxypool.proxy_set) > proxypool.proxy_set_max:
return
time.sleep(5)
#print('page', i,'done')
def get_proxy3(self):
self.new_session()
self.response = self.session.get(proxypool.proxy_html[2])
selector = etree.HTML(self.response.content)
trs = selector.xpath('//*[@id="proxylisttable"]/tbody/tr')
for tr in trs:
tds = tr.xpath('./td')
ip = tds[0].text
port = tds[1].text
proxypool.proxy_set.add(ip+':'+port)
def get_proxy4(self):
self.new_session()
self.response = self.session.get(proxypool.proxy_html[3])
selector = etree.HTML(self.response.content)
trs = selector.xpath('//*[@id="content"]/table[1]/tr')
for tr in trs[2:-2]:
tds = tr.xpath('./td')
if tds:
ip = tds[0].text
port = tds[1].xpath('./a')[0].text
#print(ip, port)
proxypool.proxy_set.add(ip+':'+port)
def pick_country(self, area = 'asia'):
if area == 'asia':
self.asia_i += 1
return proxypool.asia[self.asia_i % len(proxypool.asia)]
elif area == 'europe':
self.europe_i += 1
return proxypool.europe[self.europe_i % len(proxypool.europe)]
elif area == 'oceania':
self.oceania_i += 1
return proxypool.oceania[self.oceania_i % len(proxypool.oceania)]
elif area == 'north_america':
self.north_america_i += 1
return proxypool.north_america[self.north_america_i % len(proxypool.north_america)]
elif area == 'south_america':
self.south_america_i += 1
return proxypool.south_america[self.south_america_i % len(proxypool.south_america)]
elif area == 'africa':
self.africa_i += 1
return proxypool.africa[self.africa_i % len(proxypool.africa)]
elif area == 'world':
return proxypool.country_world
elif area == 'group':
self.group_i += 1
return proxypool.group[self.group_i % len(proxypool.group)]
else:
return list()
def get_proxy2_by_pick_country(self, area = 'asia'):
for c in self.pick_country(area):
self.get_proxy2(country = c)
def world_proxy(self):
self.get_proxy1()
self.get_proxy2_by_pick_country('world')
self.get_proxy3()
self.get_proxy4()
def eu_proxy(self):
self.get_proxy2_by_pick_country('europe')
def na_proxy(self):
self.get_proxy2_by_pick_country('north_america')
self.get_proxy3()
def taiwan_proxy(self):
self.reset_proxy()
self.get_proxy2(country ='Taiwan')
self.get_proxy4()
def asia_proxy(self):
self.get_proxy2_by_pick_country('asia')
self.get_proxy4()
def group_proxy(self):
self.get_proxy2_by_pick_country('group')
def filter_proxy(self):
bad_proxy_set = set()
for p in proxypool.proxy_set:
connection_score = list()
for t in proxypool.proxy_test:
self.new_session()
if not check_this_proxy(p):
bad_proxy_set.add(p)
else:
proxypool.proxy_set = proxypool.proxy_set - bad_proxy_set
self.proxy_set_to_proxy_list()
def check_this_proxy(self, p):
print('checking', p)
def check_proxy_on_url(p, url):
sess = requests.Session()
try:
self.response = sess.get(t, timeout=30, proxies=p)
#print('checking', p,'@', t,'get response.status_code', self.response)
sess.close()
Q.put((p, self.response.status_code == 200))
except Exception as err:
#print('checking', p,'@', t, 'Exception happened:', err)
if sess:
sess.close()
Q.put((p, False))
cpus = mp.cpu_count()
#print('cpus:', cpus)
if cpus > 1:
rounds = len(proxypool.proxy_test) // cpus
Q = mp.JoinableQueue()
for r in range(rounds):
plist = list()
for t in proxypool.proxy_test[cpus*r : cpus*(r+1)]:
pi = mp.Process(target=check_proxy_on_url, args=(p, t, cpus))
plist.append( pi )
pi.start()
Q.join()
for pi in plist:
pi.join()
for pi in plist:
pi.terminate()
del pi
else:
del plist
while not Q.empty():
check = Q.get()
#print('Q.get():', check)
if not check[1]:
print('check fail')
return False
else:
print('check pass')
return True
else:
for t in proxypool.proxy_test:
sess = requests.Session()
valid_proxy = True
try:
self.response = sess.get(t, timeout=30, proxies=p)
#print('checking', p,'@', t,'get response.status_code', self.response)
sess.close()
valid_proxy = self.response.status_code == 200
except Exception as err:
#print('checking', p,'@', t, 'Exception happened:', err)
if sess:
sess.close()
valid_proxy = False
if not valid_proxy:
print('check fail')
return False
else:
print('check pass')
return True
def random_choice_one_proxy(self):
import random
self.proxy_set_to_proxy_list()
p = random.choice(self.proxy_list)
del self.proxy_list[self.proxy_list.index(p)]
self.proxy_set.remove( list(p.values())[0] )
print('while')
while not self.check_this_proxy(p):
#print('in while')
if len(proxypool.proxy_set) == 0:
p = {'http':None}
break
p = random.choice(self.proxy_list)
del self.proxy_list[self.proxy_list.index(p)]
self.proxy_set.remove( list(p.values())[0] )
return p
def random_choice_one_proxy_with_none_freq(self):
self.none_freq_cnt += 1
if self.none_freq_cnt % self.none_freq == 0:
self.none_freq_cnt = 0
return None
else:
return self.random_choice_one_proxy()