-
Notifications
You must be signed in to change notification settings - Fork 26
/
proxy.py
372 lines (337 loc) · 13.3 KB
/
proxy.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
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Author : Eeyhan
# @File : proxy.py
import asyncio
import aiohttp
from settings import *
from copy import deepcopy
import random
from urllib.parse import urlparse, unquote
import re
from lxml import etree
import base64
import json
import aioredis
from bs4 import BeautifulSoup
import httpx
class AsyncMyRedis():
_redis = None
def __init__(self):
pass
async def get_redis_pool(self, *args, **kwargs):
if not self._redis:
self._redis = await aioredis.create_redis_pool(*args, **kwargs)
return self._redis
async def close(self):
if self._redis:
self._redis.close()
await self._redis.wait_closed()
class FreeProxy():
def __init__(self, page=5):
self.page_begin = page
self.temp_proxies = set()
self.proxies = set()
self.headers = HEADERS
self.user_agent = USER_AGENT
self.redis_client = None
self.target = self.generate_target_urls
async def get_redis_client(self):
_redis = AsyncMyRedis()
redis_client = await _redis.get_redis_pool((REDIS_HOST, REDIS_PORT), db=REDIS_DB,
encoding='utf-8')
self.redis_client = redis_client
@property
def generate_target_urls(self):
temp = deepcopy(TARGET)
target = set()
for item in temp:
if 'page' in item:
for i in range(self.page_begin):
if 'offset' in item:
offset = i * 50
url = item.format(page=i, offset=offset)
else:
url = item.format(page=i)
else:
url = item
if url and '{page}' not in url:
target.add(url)
if target:
return target
async def fetch_urls(self):
tasks = []
async with aiohttp.ClientSession() as client:
for url in self.target:
req = self.fetch(client, url)
task = asyncio.create_task(req)
tasks.append(task)
await asyncio.gather(*tasks)
async def fetch(self, client, url, *args, **kwargs):
print(12312312, url)
resp,httpx_resp = None,None
domain = urlparse(url).netloc
ua = random.choice(self.user_agent)
headers = deepcopy(self.headers)
headers['user-agent'] = ua
try:
resp = await client.get(url, headers=headers, ssl=False, timeout=250)
except (Exception, BaseException) as e:
if resp:
resp.close()
await resp.wait_for_close()
# print(12312312, e)
await asyncio.sleep(round(random.uniform(0, 5)), 2)
try:
ua = random.choice(self.user_agent)
headers = deepcopy(self.headers)
headers['user-agent'] = ua
resp = await client.get(url, headers=headers, ssl=False, timeout=250)
except (Exception, BaseException) as e:
print(44444444, e,url)
if resp:
resp.close()
await resp.wait_for_close()
return
if resp:
status = resp.status
if status == 200 or str(status).startswith('3'):
try:
html = await resp.text(encoding='utf-8')
except (Exception, BaseException) as e:
try:
html = await resp.text(encoding='gb18030')
except (Exception, BaseException) as e:
print('异常编码',e)
return
if html:
flag = TARGET_DICT.get(domain)
resp.close()
if flag:
if hasattr(self, f'parser_{flag}'):
func = getattr(self, f'parser_{flag}')
await func(html)
resp.close()
async def base64_to_ip(str):
s = base64.b64decode(str)
if s:
s = s.decode('utf-8')
if s:
return s
async def parser_www_duplichecker_com(self, res):
temp = re.findall(r'''\d+\.\d+\.\d+\.\d+\:\d+''', res, re.S | re.I)
temp = [i.strip() for i in temp if i]
if temp:
print(f'已拿到网站 duplichecker_com 的{len(temp)}条代理')
self.proxies.update(set(temp))
async def parser_free_proxy_cz(self, res):
html = etree.HTML(res)
data = html.xpath('//table[@id="proxy_list"]/tbody/tr')
end = []
for item in data:
proxy = item.xpath('./td[@class="left"]/script/text()')
proxy = ''.join(proxy).strip() if proxy else ''
if proxy:
proxy = proxy.replace('document.write(Base64.decode("', '').replace('"))', '')
proxy = await self.base64_to_ip(proxy)
if proxy:
end.append(proxy)
if end:
print(f'已拿到网站 free_proxy_cz 的{len(end)}条代理')
self.proxies.update(set(end))
async def parser_free_proxy_list_com(self, res):
html = etree.HTML(res)
data = html.xpath('//table[@class="table table-striped proxy-list"]/tbody//tr')
end = []
for item in data:
proxy = item.xpath('./td[1]/a/@title')
proxy = ''.join(proxy) if proxy else ''
end.append(proxy)
if end:
print(f'已拿到网站 free_proxy_list_com 的{len(end)}条代理')
self.proxies.update(set(end))
async def parser_www_idcloak_com(self, res):
html = etree.HTML(res)
data = html.xpath('//table[@id="sort"]/tr[position()>1]')
end = []
for item in data:
ip = item.xpath('./td[last()]/text()')
ip = ''.join(ip) if ip else ''
port = item.xpath('./td[last()-1]/text()')
port = ''.join(port) if port else ''
proxies = ip + ':' + port
if proxies:
end.append(proxies)
if end:
print(f'已拿到网站 www_idcloak_com 的{len(end)}条代理')
self.proxies.update(set(end))
async def parser_list_proxylistplus_com(self, res):
html = etree.HTML(res)
data = html.xpath('//table[@class="bg"]/tr[position()>2]')
end = []
for item in data:
ip = item.xpath('./td[2]/text()')
port = item.xpath('./td[3]/text()')
ip = ''.join(ip) if ip else ''
port = ''.join(port) if port else ''
if ip and port:
proxy = ip + ':' + port
end.append(proxy)
if end:
print(f'已拿到网站 list_proxylistplus_com 的{len(end)}条代理')
self.proxies.update(set(end))
async def parser_www_my_proxy_com(self, res):
html = etree.HTML(res)
data = html.xpath('//div[@class="list"]//text()')
data = [i.split('#')[0] for i in data if i]
if data:
print(f'已拿到网站 www_my_proxy_com 的{len(data)}条代理')
self.proxies.update(set(data))
async def parser_proxy_list_org(self, res):
html = etree.HTML(res)
data = html.xpath('//div[@class="table-wrap"]/div//ul')
end = []
for item in data:
proxy = item.xpath('./li[@class="proxy"]/script/text()')
proxy = ''.join(proxy) if proxy else ''
if proxy and 'Proxy' in proxy:
proxy = proxy.replace('Proxy', '').replace('(', '').replace(')', '')
proxy = await self.base64_to_ip(proxy)
end.append(proxy)
if end:
print(f'已拿到网站 proxy_list_org 的{len(end)}条代理')
self.proxies.update(set(end))
async def parser_proxydb_net(self, res):
html = etree.HTML(res)
data = html.xpath('//table[@class="table table-sm table-hover"]/tbody/tr')
end = []
for item in data:
proxy = item.xpath('./td[1]/a/@href')
proxy = ''.join(proxy) if proxy else ''
proxy, protocal = proxy[1:].replace('/', ':').split('#')
end.append(proxy)
if end:
print(f'已拿到网站 proxydb_net 的{len(end)}条代理')
self.proxies.update(set(end))
async def parser_www_proxynova_com(self, res):
html = etree.HTML(res)
data = html.xpath('//table[@id="tbl_proxy_list"]/tbody/tr')
end = []
for item in data:
ip = item.xpath('./td[1]/abbr/script/text()')
ip = ''.join(ip).strip() if ip else ''
if ip:
ip = ip.replace('document.write(', '').replace(');', '').replace("'", '')
port = item.xpath('./td[2]/text()')
port = ''.join(port).strip() if port else ''
if ip and port:
proxy = ip + ':' + port
end.append(proxy)
end = [i for i in end if i]
if end:
print(f'已拿到网站 www_proxynova_com 的{len(end)}条代理')
self.proxies.update(set(end))
async def parser_www_proxyrack_com(self, res):
try:
res = json.loads(res)
except json.JSONDecodeError:
return
data = res.get('records')
end = []
for item in data:
ip = item.get('ip')
port = item.get('port')
protocol = item.get('protocol')
proxies = ip + ':' + port
if proxies:
end.append(proxies)
if end:
print(f'已拿到网站 www_proxyrack_com 的{len(end)}条代理')
self.proxies.update(set(end))
async def parser_www_proxyscan_io(self, res):
html = etree.HTML(res)
data = html.xpath('//tr')
end = []
for item in data:
ip = item.xpath('./th/text()')
port = item.xpath('./td[1]/text()')
ip = ''.join(ip).strip() if ip else ''
port = ''.join(port).strip() if port else ''
if ip and port:
proxy = ip + ':' + port
end.append(proxy)
if end:
print(f'已拿到网站 www_proxyscan_io 的{len(end)}条代理')
self.proxies.update(set(end))
async def parser_api_proxyscrape_com(self, res):
res = BeautifulSoup(res, 'html.parser')
data = res.select('table[class="proxies-table"]')
if data:
data = data[0]
end = []
for item in data.children:
if item != '\n':
temp = item.select('td')
if temp:
ip = temp[0]
port = temp[1]
if ip and port:
proxy = ip.text + ':' + port.text
end.append(proxy)
if end:
print(f'已拿到网站 api_proxyscrape_com 的{len(end)}条代理')
self.proxies.update(set(end))
async def parser_smallseotools_com(self, res):
html = etree.HTML(res)
data = html.xpath('//div[@id="page-url-list"]/text()')
data = [i.strip() for i in data if i]
if data:
print(f'已拿到网站 smallseotools_com 的{len(data)}条代理')
self.proxies.update(set(data))
async def parser_www_sslproxies_org(self, res):
temp = re.findall(r'''\d+\.\d+\.\d+\.\d+\:\d+''', res, re.S | re.I)
temp = [i.strip() for i in temp if i]
if temp:
print(f'已拿到网站 www_sslproxies_org 的{len(temp)}条代理')
self.proxies.update(set(temp))
async def parser_www_socks_proxy_net(self, res):
temp = re.findall(r'''\d+\.\d+\.\d+\.\d+\:\d+''', res, re.S | re.I)
temp = [i.strip() for i in temp if i]
if temp:
print(f'已拿到网站 www_socks_proxy_net 的{len(temp)}条代理')
self.proxies.update(set(temp))
async def parser_www_proxy_list_download(self, res):
html = etree.HTML(res)
data = html.xpath('//tbody[@id="tabli"]/tr')
end = []
for item in data:
ip = item.xpath('./td[1]/text()')
port = item.xpath('./td[2]/text()')
ip = ''.join(ip) if ip else ''
port = ''.join(port) if port else ''
if ip and port:
proxy = ip + ':' + port
end.append(proxy)
if end:
print(f'已拿到网站 www_proxy_list_download 的{len(end)}条代理')
self.proxies.update(set(end))
async def save_redis(self, key=PROXY_KEY):
if self.proxies:
print(f'当前已存储代理 {len(self.proxies)} 个,正在存储')
for item in self.proxies:
await self.redis_client.sadd(key, item)
print('已存储!!!')
self.redis_client.close()
async def get_proxies(self, key=PROXY_KEY):
proxies = self.redis_client.smembers(key)
if proxies:
return proxies
async def run(self):
await self.get_redis_client()
await self.fetch_urls()
if self.proxies:
await self.save_redis()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
proxy_obj = FreeProxy()
loop.run_until_complete(proxy_obj.run())