python(爬虫翻译)

使用的火狐浏览器

import urllib.request
import urllib.parse
import json

content = input('请输入需要翻译的内容:')

url = 'http://fanyi.youdao.com/translate?
#直接从审查元素中copy过来的url会报错,必须把translate_o中的_o删除才可以
smartresult=dict&smartresult=rule'
data = {}
#复制Form Data
data['i']=content
data['from']='AUTO'
data['to']='AUTO'
data['smartresult']='dict'
data['client']='fanyideskweb'
data['salt']='15645795948636'
data['sign']='b0ddd6265192b5157527be38a121c41c'
data['ts']='1564579594863'
data['bv']='e2a78ed30c66e16a857c5b6486a1d326'
data['doctype']='json'
data['version']='2.1'
data['keyfrom']='fanyi.web'
data['action']='FY_BY_CLICKBUTTION'

data = urllib.parse.urlencode(data).encode('utf-8')

response = urllib.request.urlopen(url,data)
html = response.read().decode('utf-8')

targer = json.loads(data)
print('翻译的结果是:%s' % (target['translateResult'][0][0]['tgt']))

运行结果:

请输入需要翻译的内容:我太难了
翻译结果:I was too difficult

你可能感兴趣的:(python程序)