-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain3.py
executable file
·32 lines (25 loc) · 1.05 KB
/
main3.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
import requests
from urllib.parse import unquote
import time
# input: 7700 words, inputedit, and output
# output: word, def, sentence
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0"}
# scrape definition
def naversearch(word):
url = f"https://korean.dict.naver.com/api3/koen/search?query={word}&m=mobile&range=meaning&page=1&lang=en&hid=162545087939236350"
response = requests.get(url, headers=headers)
json_response = response.json()
result_num = json_response['pagerInfo']["totalRows"]
if result_num != 0:
definition = json_response['searchResultMap']['searchResultListMap']['MEANING']['items'][0]['expEntry']
return f"{definition}\n"
else:
return f"NO RESULTS FOR {word}\n"
def output(naversearched, filename):
with open(filename, 'a', encoding='utf-8') as f:
f.writelines(naversearched)
def main():
with open("7700 alpha.txt", encoding='utf8') as f:
for line in f:
output(naversearch(line.strip()), "definitions.txt")
main()