-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtranslate.py
58 lines (48 loc) · 1.66 KB
/
translate.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
# https://krdict.korean.go.kr/chn
# 韩语翻译
import requests
import json
def translate(word = '위'):
fileName = 'translate/'+word+'.json'
try:
with open(fileName,"r") as file:
if file:
print('已经生成', file.read())
return file.read()
except:
url = "https://krdict.korean.go.kr/chn/smallDic/searchResult?nation=chn&nationCode=11&ParaWordNo=&mainSearchWord=" + word
result = requests.get(url)
content = result.text.split('<span class="word_att_type1">')[1]
phoneticSymbols = content.split('<span class="manyLang11">')
content = phoneticSymbols[1]
phoneticSymbols = phoneticSymbols[0].strip()
partOfSpeech = content.split('</span>')[0].strip()
sentence = list(filter(None, content.split('</dl>')[0].split('</dt>')[1].replace('\t', '').replace('\n', '').replace('\r', '').split('</dd>')))
data = []
for i in range(len(sentence)):
_index = len(data)
index = i % 3
text = sentence[i].split('</strong>.')
if len(text) > 1:
sentence[i] = text[1]
else:
text2 = sentence[i].split('>')
sentence[i] = len(text2) > 1 and text2[1] or text2[0]
if index == 0:
data.append({
'title': '',
'korean': '',
'chinese': ''
})
key = [
'title',
'korean',
'chinese'
][index];
data[_index-1][key] = sentence[i]
result = str({"word": word, "phoneticSymbols": phoneticSymbols, "partOfSpeech": partOfSpeech, "sentence": data}).replace("'", "\"").replace('None', '""')
with open(fileName,"w") as file:
file.write(result)
print(result)
return result
translate('게')