-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.py
250 lines (202 loc) · 8.52 KB
/
parser.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
# coding=utf-8
"""各代理网站解析器。"""
from datetime import datetime
class ParserKuai:
index_url = 'http://www.kuaidaili.com/free/inha/1/'
name = 'kuai'
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7',
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
'Upgrade-Insecure-Requests': '1',
'Host': 'www.kuaidaili.com',
'User-Agent': 'Mozilla / 5.0(WindowsNT10.0;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) '
'Chrome / 61.0.3163.100Safari / 537.36'
}
@classmethod
def get_proxies_data(cls, tree):
return tree.xpath('//div[@id="list"]//tbody/tr')
@classmethod
def get_proxies(cls, proxy_data):
ip = cls.get_ip(proxy_data)
port = cls.get_port(proxy_data)
address = cls.get_address(proxy_data)
protocol = cls.get_protocol(proxy_data)
response_times = cls.get_response_times(proxy_data)
src = cls.name
return (dict(ip=ip, port=port, address=address, protocol=protocol, src=src, response_times=response_times),)
@classmethod
def get_ip(cls, proxy_data):
return proxy_data.xpath('./td[1]/text()')[0]
@classmethod
def get_port(cls, proxy_data):
return proxy_data.xpath('./td[2]/text()')[0]
@classmethod
def get_address(cls, proxy_data):
return proxy_data.xpath('./td[5]/text()')[0]
@classmethod
def get_protocol(cls, proxy_data):
return proxy_data.xpath('./td[4]//text()')[0]
@classmethod
def get_response_times(cls, proxy_data):
try:
response_times = float(proxy_data.xpath('./td[6]/text()')[0].replace('秒', '')) * 1000
except ValueError:
# 响应速度只有「秒」,没写数字
response_times = 1
return response_times
@classmethod
def get_verify_datetime(cls, proxy_data):
return datetime.strptime(proxy_data.xpath('./td[7]/text()')[0], '%Y-%m-%d %H:%M:%S')
class ParserXici:
index_url = 'http://www.xicidaili.com/nn/1'
name = 'xici'
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7',
'Cache-Control': 'max-age=0',
'Upgrade-Insecure-Requests': '1',
'Host': 'www.xicidaili.com',
'User-Agent': 'Mozilla / 5.0(WindowsNT10.0;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) '
'Chrome / 61.0.3163.100Safari / 537.36'
}
@classmethod
def get_proxies_data(cls, tree):
return tree.xpath('//table[@id="ip_list"]/tr[@class]')
@classmethod
def get_proxies(cls, proxy_data):
ip = cls.get_ip(proxy_data)
port = cls.get_port(proxy_data)
address = cls.get_address(proxy_data)
protocol = cls.get_protocol(proxy_data)
response_times = cls.get_response_times(proxy_data)
src = cls.name
return (dict(ip=ip, port=port, address=address, protocol=protocol, src=src, response_times=response_times),)
@classmethod
def get_ip(cls, proxy_data):
return proxy_data.xpath('./td[2]//text()')[0]
@classmethod
def get_port(cls, proxy_data):
return proxy_data.xpath('./td[3]//text()')[0]
@classmethod
def get_address(cls, proxy_data):
return proxy_data.xpath('./td[4]//text()')[1] if proxy_data.xpath('./td[4][./a]') else ''
@classmethod
def get_protocol(cls, proxy_data):
return proxy_data.xpath('./td[6]//text()')[0]
@classmethod
def get_response_times(cls, proxy_data):
return float(proxy_data.xpath('./td[7]/div[@title]/@title')[0].replace('秒', '')) * 1000
@classmethod
def get_verify_datetime(cls, proxy_data):
return datetime.strptime(proxy_data.xpath('./td[10]//text()')[0], '%y-%m-%d %H:%M')
class ParserGoubanjia:
index_url = 'http://www.goubanjia.com/free/gngn/index1.shtml'
name = 'goubanjia'
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7',
'Cache-Control': 'max-age=0',
'Upgrade-Insecure-Requests': '1',
'Host': 'www.goubanjia.com',
'User-Agent': 'Mozilla / 5.0(WindowsNT10.0;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) '
'Chrome / 61.0.3163.100Safari / 537.36',
'Connection': 'keep-alive'
}
@classmethod
def get_proxies_data(cls, tree):
return tree.xpath('//tbody/tr')
@classmethod
def get_proxies(cls, proxy_data):
ip = cls.get_ip(proxy_data)
port = cls.get_port(proxy_data)
address = cls.get_address(proxy_data)
protocol = cls.get_protocol(proxy_data)
response_times = cls.get_response_times(proxy_data)
src = cls.name
return (dict(ip=ip, port=port, address=address, protocol=protocol, src=src, response_times=response_times),)
@classmethod
def get_ip(cls, proxy_data):
return ''.join(proxy_data.xpath(
'./td[1]//node()[(@style and (@style!="display: none;" and @style!="display:none;")) or not(@style)]/text() | ./td[1]/text()')).split(
':')[0]
@classmethod
def get_port(cls, proxy_data):
def get_port(word):
num_list = []
for item in word:
num = 'ABCDEFGHIZ'.find(item)
num_list.append(str(num))
port = int("".join(num_list)) >> 0x3
return str(port)
return get_port(proxy_data.xpath('.//node()[contains(@class, "port")]/@class')[0].split()[1])
@classmethod
def get_address(cls, proxy_data):
return ''.join(proxy_data.xpath('./td[4]/a/text()'))
@classmethod
def get_protocol(cls, proxy_data):
return proxy_data.xpath('./td[3]//text()')[0]
@classmethod
def get_response_times(cls, proxy_data):
return float(proxy_data.xpath('./td[6]//text()')[0].replace(' 秒', '')) * 1000
class ParserCoderbusy:
index_url = 'https://proxy.coderbusy.com/zh-cn/classical/anonymous-type/highanonymous/p1.aspx'
name = 'coderbusy'
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7',
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
'Upgrade-Insecure-Requests': '1',
'Host': 'proxy.coderbusy.com',
'User-Agent': 'Mozilla / 5.0(WindowsNT10.0;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) '
'Chrome / 61.0.3163.100Safari / 537.36'
}
@classmethod
def get_proxies_data(cls, tree):
return tree.xpath('//tbody/tr')
@classmethod
def get_proxies(cls, proxy_data):
ip = cls.get_ip(proxy_data)
port = cls.get_port(proxy_data)
address = cls.get_address(proxy_data)
protocol = cls.get_protocol(proxy_data)
response_times = cls.get_response_times(proxy_data)
src = cls.name
if cls.is_suppose_https(proxy_data):
return (dict(ip=ip, port=port, address=address, protocol=protocol, src=src, response_times=response_times),
dict(ip=ip, port=port, address=address, protocol='https', src=src, response_times=response_times))
else:
return (dict(ip=ip, port=port, address=address, protocol=protocol, src=src, response_times=response_times),)
@classmethod
def get_ip(cls, proxy_data):
return proxy_data.xpath('./td[1]//text()')[1].strip()
@classmethod
def get_port(cls, proxy_data):
return proxy_data.xpath('./td[2]/text()')[0]
@classmethod
def get_address(cls, proxy_data):
return proxy_data.xpath('./td[3]//text()')[1]
@classmethod
def get_protocol(cls, proxy_data):
return proxy_data.xpath('./td[5]//text()')[0]
@classmethod
def get_response_times(cls, proxy_data):
return float(proxy_data.xpath('./td[10]//text()')[0].replace('秒', '')) * 1000
@classmethod
def is_suppose_https(cls, proxy_data):
if proxy_data.xpath('./td[8]/span'):
return True
else:
return False
parsers = [
ParserKuai,
ParserXici,
ParserGoubanjia,
ParserCoderbusy
]