-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
347 lines (282 loc) · 10.4 KB
/
main.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
import requests
from lxml import etree
import re
import pymysql
host = 'https://www.infineon.com'
conn = pymysql.connect(host='localhost', port=3306, user="your_user", passwd='your_pwd', charset='utf8mb4', database="isee")
cur = conn.cursor()
ctable = 'create table plist(id int primary key auto_increment , category varchar(100), cid int)'
dctable = 'drop table plist'
ptable = 'create table product(id int primary key auto_increment, name varchar(100), min float, max float, instruct varchar(400), package varchar(50), pdf varchar(200), cid int not null)'
dptable = 'drop table product'
cur.execute(dctable)
cur.execute(dptable)
conn.commit()
cur.execute(ctable)
cur.execute(ptable)
conn.commit()
# 三次失败重试
def get_response(url):
count = 0
while count < 3:
try:
if count == 0:
req = requests.get(url=url, timeout=60)
else:
req = requests.get(url=url, timeout=30)
return req
except Exception as e:
count += 1
# 获取除了ASIC, BatteryManagement, 高可靠性器件之外的顶级分类信息
def get_top_ctg() -> list:
req = get_response('https://www.infineon.com/cms/cn/services/ajax/navigationsection.html?path=%2Fen%2Fproduct%2F.content%2Fnavigationsection%2Fn_00002.html')
tree = etree.HTML(req.text)
div_lst = tree.xpath("//div[@class='col-xxs-6 col-md-7']/div")
lst = []
for i in div_lst:
res = str(i.xpath("./ul/li[1]/a/text()")[0])
href = i.xpath("./ul/li[1]/a/@href")
if res.startswith('ASIC') or res.startswith('Battery') or res.startswith('高可靠性器件') or res.startswith('Wireless') or res.startswith('Universal') or res.startswith('Clocks'):
continue
lst.append([res, host + href[0]])
return lst
# 获取料号
def get_title(tree):
title = tree.xpath("//h1[@class='page-title']/span/text()")
if title:
return title[0]
return 'NULL'
# 获取产品表中产品的URL, text为产品表的JSON文本
def get_product_table(text):
res = re.findall('"openCmsPath":"(.*?)"', text, re.S)
for i in range(0, len(res)):
res[i] = host + res[i]
return res
# 提取产品表URL, text为当前页面的html源代码
def get_product_table_url(text: str) -> str:
res = re.findall(
'{"tableConfigId":"(.*?)","collectionId":"(.*?)","collectionType":"(.*?)","showAllOPNs":(.*?)}',
text, re.S)
url = 'https://www.infineon.com/products/pc/frontendprecalculation?tableconfigid=' + res[0][0] + '&collectionid=' + res[0][1] + '&collectiontype=' + res[0][2] + '&showallopns=' + 'true' + '&calculatefilters=true'
return url
# 去除空格之外的空白
def no_space(text: str) -> str:
return text.strip()
# 判断是否为产品列表的第一种方法
def are_you_plist1(tree) -> bool:
rst = tree.xpath("//section[@class='subcategories']")
if rst:
return True
else:
return False
# 判断是否为产品列表的第二种方法
def are_you_plist2(text: str) -> bool:
res = re.findall(
'{"tableConfigId":"(.*?)","collectionId":"(.*?)","collectionType":"(.*?)","showAllOPNs":(.*?)}',
text, re.S)
if res:
return True
else:
return False
# 判断是否为产品列表
def is_plist(text) -> bool:
tree = etree.HTML(text=text)
if are_you_plist1(tree) or are_you_plist2(text=text):
return True
else:
return False
# 获取子菜单的子菜单,过滤掉综述,防止爬取到重复数据
def get_url1(tree) -> list:
sub_urls = tree.xpath('//ul[contains(@class,"subcategoryNavColumn__sublist")]/li/a/@href')
sub_text = tree.xpath('//ul[contains(@class,"subcategoryNavColumn__sublist")]/li/a/text()')
new_list = []
for i in range(0, len(sub_text)):
if sub_text[i] != '综述' and sub_text[i] != 'Overview':
new_list.append(host + sub_urls[i])
return new_list
# 抓取子菜单的条目,过滤掉url为javascript的子菜单
def get_url2(tree) -> list:
rst = tree.xpath("//li[@class='subcategoryNavColumn__item']/a/@href")
new_list = []
for i in rst:
if i != 'javascript:':
new_list.append(host + i)
return new_list
# 获取简介
def get_instruct(tree):
rst = tree.xpath("//section[@class='content']/p[@class='h2']/text()")
if rst:
return rst[0]
return 'NULL'
# 获取ispnID号, 用于获取指标参数
def get_ispnid(text):
rst = re.findall("ispnId: '(.*?)'", text, re.S)
if rst:
return rst[0]
return ''
# 获取pdf链接
def get_pdf_link(tree) -> str:
link_lst = tree.xpath("//meta[@name='doc_url']/@content")
if link_lst:
return host + link_lst[0]
return 'NULL'
# 获取指标参数URL
def get_param_url(text):
url = get_ispnid(text)
return 'https://www.infineon.com/products/pc/parametrics?lang=en&ispnId=' + url
# 匹配到就返回匹配到的值,匹配不到返回None
def get_package(text: str):
ppos = text.rfind('"nameFormatted":"Package"')
spos = text.rfind('stringValue', 0, ppos)
rst = re.search('stringValue":"(.*?)"', text[spos: ppos], re.S)
if rst:
return rst.groups()[0]
else:
return None
# 获取温度指标。如果匹配成功,则返回值为3个数,第一个数是最高温度,第二个数是最低温度
def get_temperature(text: str):
tpos = text.rfind("Operating Temperature")
if tpos < 0:
return ['NULL', 'NULL']
vpos = text.rfind('minValueBaseUnit', 0, tpos)
mpos = text.rfind('maxValueBaseUnit', 0, tpos)
if vpos < 0 and mpos < 0:
return ['NULL', 'NULL']
info_str = text[vpos: tpos]
min_rst = re.search('"maxValueDisplayUnit":.*?,"nameFormatted":".*?",.*?"minValueDisplayUnit":(.*?),', info_str, re.S)
max_rst = re.search('"maxValueDisplayUnit":(.*?),"nameFormatted":".*?",.*?"minValueDisplayUnit":.*?,', info_str, re.S)
if min_rst:
if min_rst.groups()[0] == 'null':
min_rst = 'NULL'
else:
min_rst = float(min_rst.groups()[0])
else:
min_rst = 'NULL'
if max_rst:
if max_rst.groups()[0] == 'null':
max_rst = 'NULL'
else:
max_rst = float(max_rst.groups()[0])
else:
max_rst = 'NULL'
return [max_rst, min_rst]
# 获取需要的参数
def get_target_data(text):
tree = etree.HTML(text)
name = get_title(tree)
instruct = no_space(get_instruct(tree))
url = get_param_url(text)
req = get_response(url)
package = get_package(req.text)
lst = get_temperature(req.text)
pdf = get_pdf_link(tree)
maxtmp = lst[0]
mintmp = lst[1]
return {'min': mintmp, 'max': maxtmp, 'name': name, 'instruct': instruct, 'package': package, 'pdf': pdf}
# 第一种获取产品列表的方法
# return: 返回一个列表,其每个元素都是子产品的url
def get_plist1(tree) -> list:
u1 = get_url1(tree)
u2 = get_url2(tree)
u1.extend(u2)
return list(set(u1))
# 第二种获取产品列表的方法
def get_plist2(text):
url = get_product_table_url(text)
req = get_response(url=url)
urls = get_product_table(req.text)
return urls
# 获取产品列表。该方法先调用第一种获取产品列表的方法,再调用第二种获取产品列表的方法。之所以这么做是因为先调用第二种方法会引起分类混乱
def get_plist(text: str):
tree = etree.HTML(text)
res = get_plist1(tree)
if res:
return res
res = get_plist2(text)
if res:
return res
return None
# 保存数据到产品表
def save_plist(name: str, father_id: int) -> int:
sql = 'select id from plist where category=%s&&cid=%s'
cur.execute(sql, (name, father_id))
res = cur.fetchone()
if not res:
sql = "insert into plist (category, cid)values(%s, %s)"
cur.execute(sql, (name, father_id))
conn.commit()
sql = 'select id from plist where category=%s&&cid=%s'
cur.execute(sql, (name, father_id))
res = cur.fetchone()
return res[0]
else:
return res[0]
# 将当前产品的标题存入数据库
def process_plist(text: str, father_id: int) -> list:
rst = get_plist(text)
tree = etree.HTML(text)
name = get_title(tree)
cid = save_plist(name=name, father_id=father_id)
print("Save category: " + name)
return [rst, cid]
# 把数据存入数据库
def save_into_product(dct: dict, father_id: int):
sql = 'insert into product (name, min, max, instruct, package, pdf, cid)values(%s, %s, %s, %s, %s, %s, %s)'
if dct['min'] == 'NULL':
dct['min'] = None
if dct['max'] == 'NULL':
dct['max'] = None
if dct['instruct'] == 'NULL':
dct['instruct'] = None
if dct['package'] == 'NULL':
dct['package'] = None
print(dct)
try:
cur.execute(sql, (dct['name'], dct['min'], dct['max'], dct['instruct'], dct['package'], dct['pdf'], father_id))
conn.commit()
print('Save Success.')
except Exception as e:
print(e)
# 下载pdf
def dl_pdf(url, name):
name = 'infineon' + '--' + name + '.pdf'
req = get_response(url)
with open("./pdf_files/" + name, 'wb') as f:
f.write(req.content)
def process_product(text: str, father_id: int):
data = get_target_data(text)
if data['pdf'] == 'NULL' or data['name'] == 'NULL':
return 0
dl_pdf(data['pdf'], data['name'])
data['pdf'] = 'infineon' + '--' + data['name'] + '.pdf'
save_into_product(data, father_id)
def wrong_log(e, url):
with open("./wrong_log.log", 'a') as f:
f.write(url)
f.write('\n')
f.write(str(e))
f.write('\n')
def main(url, father_id):
try:
res = get_response(url=url)
print("Request URL: ", url)
if res.status_code == 404:
print("Request URL Error: 404: " + url)
return 0
elif is_plist(res.text):
data = process_plist(res.text, father_id)
for pct in data[0]:
main(pct, father_id=data[1])
else:
process_product(res.text, father_id)
except Exception as e:
print('*' * 20)
print(e)
print('*' * 20)
wrong_log(e, url)
top_lst = get_top_ctg()
for lt in top_lst:
tid = save_plist(lt[0], 0)
main(lt[1], tid)
cur.close()
conn.close()