Implementation of Chinese English Translation Dictionary with python Crawler
- 2021-07-01 07:45:55
- OfStack
In this paper, we share the specific code of python crawler to realize Chinese-English translation dictionary for your reference, the specific contents are as follows
According to the translation resources of a certain platform, the translation information is extracted and displayed, including three processes: input, translation and output, which are mainly realized by python language (python3.6).
import urllib.request
import urllib.parse
import json
def en_zh(content):
url = 'http://fanyi.baidu.com/v2transapi'
head = {}
head['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
data={}
data['from'] = 'en'
data['to'] = 'zh'
data['query'] = content
data['transtype'] = 'translang'
data['simple_means_flag'] = '3'
data = urllib.parse.urlencode(data).encode('utf-8')
req =urllib.request.Request(url,data,head)
response=urllib.request.urlopen(req)
html = response.read().decode('utf-8')
target = json.loads(html)
print(" Translation results: %s" %(target['trans_result']['data'][0]['dst']))
def zh_en(content):
url = 'http://fanyi.baidu.com/v2transapi'
data={}
data['from'] = 'zh'
data['to'] = 'en'
data['query'] = content
data['transtype'] = 'translang'
data['simple_means_flag'] = '3'
data = urllib.parse.urlencode(data).encode('utf-8')
req =urllib.request.Request(url,data)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36')
response=urllib.request.urlopen(req)
html = response.read().decode('utf-8')
target = json.loads(html)
print(" Translation results: %s" %(target['trans_result']['data'][0]['dst']))
while(True):
content = input(" Please enter what you want to translate ( Press q Quit ) : ")
if content=='q':
input(" You have quit, welcome to use again ")
break
en_zh(content)
zh_en(content)