-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvknsorgula_spider.py
More file actions
211 lines (180 loc) · 6.12 KB
/
Copy pathvknsorgula_spider.py
File metadata and controls
211 lines (180 loc) · 6.12 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/11 7:29
# @Author : GuoChang
# @Site : https://github.com/xiphodon
# @File : vknsorgula_spider.py
# @Software: PyCharm
import asyncio
import json
from pathlib import Path
from gevent import pool, monkey;
monkey.patch_all()
from WhileRequests import WhileRequests
from base_spider import BaseSpider, DataProgress
import pandas as pd
from lxml import etree
import html
from urllib import parse
def r(e, t):
"""
取两个字符,并转为16进制数字
:param e:
:param t:
:return:
"""
_r = e[t: t + 2]
_ri = int(_r, 16)
# print(_r, _ri)
return _ri
def de_email(n, c):
"""
解密邮件
:param n:
:param c:
:return:
"""
o = ''
a = r(n, c)
for i in range(2, len(n), 2):
m = r(n, i) ^ a
o += chr(m)
t = parse.unquote(html.escape(o))
# print(t)
return t
class VknSpider(BaseSpider):
"""
vkn 爬虫
"""
data_dir_path = Path(r'E:\workspace_all\workspace_py\hs_code_spider\data\土耳其')
json_dir_path = data_dir_path / '土耳其.json'
home_dir = Path(r'E:\vkn')
def __init__(self):
"""
初始化
"""
super(BaseSpider, self).__init__()
self.data = list()
self.home_url = 'https://vknsorgula.net/single.php'
self.dp = DataProgress()
self.requests = WhileRequests()
def load_data(self):
"""
加载数据
:return:
"""
if self.json_dir_path.exists():
with open(self.json_dir_path.as_posix(), 'r', encoding='utf8') as fp:
self.data = json.load(fp)
print(len(self.data))
return
# data_list = list()
data_set = set()
for file_path in self.data_dir_path.iterdir():
df = pd.read_csv(file_path.as_posix(), dtype='str')
# print(df.head(100))
item_list = df.iloc[:, 1].tolist()
data_set.update((i for i in item_list if not pd.isna(i)))
data = list(data_set)
with open(self.json_dir_path.as_posix(), 'w', encoding='utf8') as fp:
json.dump(data, fp)
self.data = data
def download_pages(self):
"""
下载页面
:return:
"""
dp = DataProgress()
data_len = len(self.data)
for i, item in enumerate(self.data, start=1):
dp.print_data_progress(i, data_len)
file_name = f'{item}.json'
file_path = self.home_dir / file_name
if file_path.exists():
continue
result = self.requests.get(url=f'{self.home_url}?vkn={item}')
# print(result.text)
selector = etree.HTML(result.text)
no = self.data_list_get_first(selector.xpath('.//div[@class="section-title"]/h1[@class="mb-5"]/text()'), '')
name = self.data_list_get_first(selector.xpath('.//div[@class="section-title"]/h2[@class="mb-5"]/text()'))
date_time = self.data_list_get_first(selector.xpath('.//div[@class="section-title"]/h4[@class="mb-5"]/text()'))
email = self.data_list_get_first(selector.xpath('.//div[@class="section-title"]/h4[@class="mb-5"]/a/@data-cfemail'))
if email:
email = de_email(email, 0)
item_data = {
'no': no,
'name': name,
'date_time': date_time,
'email': email
}
# print(item_data)
with open(file_path, 'w', encoding='utf8') as fp:
json.dump(item_data, fp)
# return
def download_page(self, data):
"""
多协程下载页面
:param data:
:return:
"""
i, item, total_len, dp = data
dp.print_data_progress(i, total_len)
file_name = f'{item}.json'
file_path = self.home_dir / file_name
if file_path.exists():
return
result = self.requests.get(url=f'{self.home_url}?vkn={item}')
# print(result.text)
selector = etree.HTML(result.text)
no = self.data_list_get_first(selector.xpath('.//div[@class="section-title"]/h1[@class="mb-5"]/text()'), '')
name = self.data_list_get_first(selector.xpath('.//div[@class="section-title"]/h2[@class="mb-5"]/text()'))
date_time = self.data_list_get_first(selector.xpath('.//div[@class="section-title"]/h4[@class="mb-5"]/text()'))
email = self.data_list_get_first(
selector.xpath('.//div[@class="section-title"]/h4[@class="mb-5"]/a/@data-cfemail'))
if email:
email = de_email(email, 0)
item_data = {
'no': no,
'name': name,
'date_time': date_time,
'email': email
}
# print(item_data)
with open(file_path, 'w', encoding='utf8') as fp:
json.dump(item_data, fp)
def gevent_pool_download_pages(self, pool_size: int = 20):
"""
多协程下载页面
:return:
"""
self.load_data()
data_list = self.data
dp = DataProgress()
gevent_pool = pool.Pool(pool_size)
result_list = gevent_pool.map(self.download_page,
[(i, item, len(data_list), dp) for i, item in enumerate(data_list, start=1)])
return result_list
def merge_data(self):
"""
合并数据
:return:
"""
df = pd.DataFrame(columns=['no', 'name', 'date_time', 'email'])
for i, file_path in enumerate(self.home_dir.iterdir()):
if file_path.suffix != '.json':
continue
with open(file_path.as_posix(), 'r', encoding='utf8') as fp:
item_data = json.load(fp)
no = str(item_data['no'])
if no.strip() == '':
continue
print(i)
df = df.append([item_data], ignore_index=True)
# if i > 100:
# break
df.to_csv(r'E:\vkn\data.csv')
if __name__ == '__main__':
vs = VknSpider()
# vs.download_pages()
# vs.gevent_pool_download_pages(pool_size=40)
vs.merge_data()