-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_all_spider_data.py
More file actions
323 lines (267 loc) · 11.2 KB
/
Copy pathmerge_all_spider_data.py
File metadata and controls
323 lines (267 loc) · 11.2 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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2017/11/22 9:35
# @Author : GuoChang
# @Site : https://github.com/xiphodon
# @File : merge_all_spider_data.py
# @Software: PyCharm
import company_spider_3
import company_spider_4
import company_spider_5
import company_spider_6
import json
import conn_sql_server
import os
home_path = r'E:\work_all\topease\company_spider_merge'
all_company_json_path = os.path.join(home_path, r'all_company_data.json')
all_company_json_path_1 = os.path.join(home_path, r'all_company_data_1.json')
all_company_json_path_2 = os.path.join(home_path, r'all_company_data_2.json')
# company_id 计数标记
company_id = 0
def merge_all_json_data():
"""
合并所有的json数据,统一格式
:return:
"""
all_data_json = []
merge_spider_3_data(all_data_json)
merge_spider_5_data(all_data_json)
# OOM 异常
# with open(all_company_json_path_1, 'w', encoding='utf8') as fp:
# fp.write(json.dumps(all_data_json))
#
# all_data_json = []
merge_spider_4_data(all_data_json)
merge_spider_6_data(all_data_json)
# with open(all_company_json_path_2, 'w', encoding='utf8') as fp:
# fp.write(json.dumps(all_data_json))
with open(all_company_json_path, 'w', encoding='utf8') as fp:
fp.write('[')
for item_dict in all_data_json:
fp.write(json.dumps(item_dict) + ',')
fp.seek(fp.tell()-1, 0)
fp.write(']')
def merge_all_json_data_2():
"""
合并all_data_part_1 和 all_data_part_2数据,统一格式
:return:
"""
all_data_json = []
merge_spider_all_data_1(all_data_json)
print('merge_spider_all_data_1', 'OK')
merge_spider_all_data_2(all_data_json)
print('merge_spider_all_data_2', 'OK')
with open(all_company_json_path, 'w', encoding='utf8') as fp:
fp.write('[')
for item_dict in all_data_json:
fp.write(json.dumps(item_dict) + ',')
fp.seek(fp.tell()-1, 0)
fp.write(']')
def read_all_company_json():
"""
读取合并好的所有公司json
:return:
"""
with open(all_company_json_path, 'r', encoding='utf8') as fp:
data = json.loads(fp.read())
return data
def merge_spider_3_data(all_data_json):
"""
合并spider_3 json数据
:param all_data_json:
:return:
"""
global company_id
data_3 = company_spider_3.read_company_desc_list_has_web_json()
for item_dict in data_3:
item_dict = conn_sql_server.check_dict(item_dict)
company_id += 1
temp_dict = dict()
temp_dict['company_id'] = company_id
temp_dict['_from_spider'] = 3
temp_dict['Name'] = item_dict.get('company_name', '')
temp_dict['Description'] = item_dict.get('company_description', '')
temp_dict['Country'] = item_dict.get('company_country', '')
temp_dict['City'] = item_dict.get('company_city', '')
temp_dict['Address'] = item_dict.get('company_adrress', '')
temp_dict['Website'] = item_dict.get('company_web_origin', '')
temp_dict['MainProduct'] = item_dict.get('company_product', '')
temp_dict['Telephone'] = check_phone(item_dict.get('company_telephone', ''))
temp_dict['Type'] = 'buyer or seller'
print(temp_dict['_from_spider'], company_id)
all_data_json.append(temp_dict)
# if company_id == 20:
# break
def merge_spider_4_data(all_data_json):
"""
合并spider_4 json数据
:param all_data_json:
:return:
"""
global company_id
data_4 = company_spider_4.read_company_desc_has_phone_str_json()
for item_dict in data_4:
item_dict = conn_sql_server.check_dict(item_dict)
company_id += 1
temp_dict = dict()
temp_dict['company_id'] = company_id
temp_dict['_from_spider'] = 4
temp_dict['Name'] = item_dict.get('company_name', '')
temp_dict['Description'] = item_dict.get('company_desc', '')
temp_dict['Country'] = item_dict.get('Country/Region', '')
temp_dict['City'] = item_dict.get('Location', '')
temp_dict['Address'] = item_dict.get('Address', '')
temp_dict['Website'] = item_dict.get('Website', '')
temp_dict['MainProduct'] = item_dict.get('Main Products', '')
temp_dict['Fax'] = check_phone(item_dict.get('Fax Number', ''))
temp_dict['Telephone'] = check_phone(item_dict.get('Telephone', ''))
temp_dict['CustomerPhone'] = check_phone(item_dict.get('Mobilephone', ''))
temp_dict['SalesVolume'] = item_dict.get('Total Annual Sales Volume', '')
temp_dict['MainMarkets'] = item_dict.get('Main Markets', '')
temp_dict['PostCode'] = item_dict.get('Zip/Post Code', '')
temp_dict['BusinessType'] = item_dict.get('Business Type', '')
temp_dict['YearStartExporting'] = item_dict.get('Year Start Exporting', '')
temp_dict['ContactPerson'] = item_dict.get('Contact Person', '')
temp_dict['JobTitle'] = item_dict.get('Job Title', '')
temp_dict['OfficeAddress_Detail'] = item_dict.get('Operational Address', '')
temp_dict['Department'] = item_dict.get('Department', '')
temp_dict['TradeCapacity'] = item_dict.get('Trade Capacity', '')
temp_dict['ProductionCapacity'] = item_dict.get('Production Capacity', '')
temp_dict['AverageLeadTime'] = item_dict.get('Average Lead Time', '')
temp_dict['ContractManufacturing'] = item_dict.get('Contract Manufacturing', '')
temp_dict['RegisteredCapital'] = item_dict.get('Registered Capital', '')
temp_dict['RDCapacity'] = item_dict.get('R&D; Capacity', '')
temp_dict['LegalRepresentative'] = item_dict.get('Legal Representative / CEO', '')
temp_dict['QCStaff'] = item_dict.get('No. of QC Staff', '')
temp_dict['QualityControl'] = item_dict.get('Quality Control', '')
temp_dict['YearEstablished'] = item_dict.get('Year Established', '')
temp_dict['Certificates'] = item_dict.get('Certificates', '')
temp_dict['Revenue'] = item_dict.get('Total Revenue', '')
temp_dict['NumberOfEmployess'] = item_dict.get('Number Of Employess', '')
temp_dict['Type'] = 'buyer or seller'
print(temp_dict['_from_spider'], company_id)
all_data_json.append(temp_dict)
# if company_id == 40:
# break
def merge_spider_5_data(all_data_json):
"""
合并spider_5 json数据
:param all_data_json:
:return:
"""
global company_id
data_5 = company_spider_5.read_company_desc_list_json()
for item_dict in data_5:
item_dict = conn_sql_server.check_dict(item_dict)
company_id += 1
temp_dict = dict()
temp_dict['company_id'] = company_id
temp_dict['_from_spider'] = 5
temp_dict['Industry'] = item_dict.get('category', '')
temp_dict['Name'] = item_dict.get('company_name', '')
temp_dict['Introduction'] = item_dict.get('company_introduction', '')
temp_dict['Description'] = item_dict.get('company_desc_en', '')
temp_dict['Description_cn'] = item_dict.get('company_desc_cn', '')
temp_dict['Country'] = item_dict.get('country_or_area_en', '')
temp_dict['Country_cn'] = item_dict.get('country_or_area_cn', '')
temp_dict['Address'] = item_dict.get('company_address', '')
temp_dict['Website'] = item_dict.get('company_web', '')
temp_dict['Email'] = item_dict.get('company_email', '')
temp_dict['Fax'] = check_phone(item_dict.get('company_fax', ''))
temp_dict['Telephone'] = check_phone(item_dict.get('company_telephone', ''))
temp_dict['PostCode'] = item_dict.get('post_code', '')
temp_dict['ContactPerson'] = item_dict.get('contact_person', '')
temp_dict['NumberOfEmployess'] = item_dict.get('employee_count', '')
temp_dict['Type'] = 'buyer'
print(temp_dict['_from_spider'], company_id)
all_data_json.append(temp_dict)
# if company_id == 60:
# break
def merge_spider_6_data(all_data_json):
"""
合并spider_6 json数据
:param all_data_json:
:return:
"""
global company_id
data_6 = company_spider_6.read_company_desc_list_json()
for item_dict in data_6:
item_dict = conn_sql_server.check_dict(item_dict)
company_id += 1
temp_dict = dict()
temp_dict['company_id'] = company_id
temp_dict['_from_spider'] = 6
temp_dict['Industry'] = item_dict.get('industry_category', '')
Name_1 = item_dict.get('Company', '')
Name_2 = item_dict.get('Company Name (公司名称)', '')
temp_dict['Name'] = Name_1 if Name_1 != '' else Name_2
temp_dict['Description'] = item_dict.get('content_desc_en', '')
temp_dict['Description_cn'] = item_dict.get('content_desc_cn', '')
temp_dict['Country'] = item_dict.get('Country(国家地区)', '')
Address_1 = item_dict.get('Address', '')
Address_2 = item_dict.get('Address (地址)', '')
temp_dict['Address'] = Address_1 if Address_1 != '' else Address_2
Website_1 = item_dict.get('Website', '')
Website_2 = item_dict.get('Website (网站)', '')
temp_dict['Website'] = Website_1 if Website_1 != '' else Website_2
temp_dict['Email'] = item_dict.get('Email Address (电子邮件)', '')
Fax_1 = item_dict.get('Fax', '')
Fax_2 = item_dict.get('Fax (传真)', '')
temp_dict['Fax'] = check_phone(Fax_1 if Fax_1 != '' else Fax_2)
Telephone_1 = item_dict.get('Tel', '')
Telephone_2 = item_dict.get('Tel (电话)', '')
temp_dict['Telephone'] = check_phone(Telephone_1 if Telephone_1 != '' else Telephone_2)
ContactPerson_1 = item_dict.get('Contact Person', '')
ContactPerson_2 = item_dict.get('Contact Person (联系人)', '')
temp_dict['ContactPerson'] = ContactPerson_1 if ContactPerson_1 != '' else ContactPerson_2
temp_dict['PurchaseProduct'] = item_dict.get('Purchase Product (采购产品)', '')
temp_dict['Type'] = 'buyer'
print(temp_dict['_from_spider'], company_id)
all_data_json.append(temp_dict)
# if company_id == 80:
# break
def merge_spider_all_data_1(all_data_json):
"""
合并all_data_1
:return:
"""
with open(all_company_json_path_1, 'r', encoding='utf8') as fp:
data_1 = json.loads(fp.read())
all_data_json.extend(data_1)
def merge_spider_all_data_2(all_data_json):
"""
合并all_data_2
:return:
"""
with open(all_company_json_path_2, 'r', encoding='utf8') as fp:
data_2 = json.loads(fp.read())
all_data_json.extend(data_2)
def check_phone(phone_str):
"""
检查手机号码
:return:
"""
allow_char_list = [' ', '-', '(', ')', '+', '/', ',', ';']
new_phone_str = ''
for item_char in phone_str:
if (not is_int(item_char)) and (item_char not in allow_char_list):
item_char = ' '
new_phone_str += item_char
return new_phone_str
def is_int(char):
"""
字符是否为数字
:param char: 单字符
:return:
"""
try:
int(char)
except ValueError:
return False
else:
return True
if __name__ == '__main__':
# merge_all_json_data()
# merge_all_json_data_2()
# print(read_all_company_json()[:5])
merge_all_json_data()