-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbing.py
58 lines (48 loc) · 1.41 KB
/
bing.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
import requests
from bs4 import BeautifulSoup
def GetUrl(word):
return "https://cn.bing.com/dict/search?q=" + str(word)
def Query(word):
res = requests.get("https://cn.bing.com/dict/search?q=" + str(word))
soup = BeautifulSoup(res.text, "html.parser")
res = {}
try:
res['word'] = soup.select('.hd_div h1')[0].text
except:
res['word'] = word
try:
res['prus'] = soup.select('.hd_prUS')[0].text
except:
res['prus'] = ''
res['qdef'] = []
for item in soup.select(".qdef ul li"):
res['qdef'].append({'pos': item.select(".pos")[0].text,
'def': item.select(".def span")[0].text})
return res
def QueryList(words, f=print):
lines = ''
for word_ in words:
word = word_.strip()
if len(word) == 0:
continue
f(word)
try:
res = Query(word)
line = '<tr><td>'
line += res['word']
line += '</td><td>'
line += res['prus']
line += '</td><td>'
for item in res['qdef']:
line += item['pos']
if item['pos'] == '网络':
line += '.'
line += item['def']
line += '。'
line += '</tr>'
lines += line
except Exception as e:
f('Error.')
f(e)
f('Done.')
return lines