|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import urllib.request
import urllib.parse
import json
import time
#利用有道词典翻译
while True:
count = input('请输入需要翻译的词汇:')
if count=='q':
break
url = 'http://fanyi.youdao.com/translate'
head = {}#user-agent字典
head['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393'
#模拟人类操作。,避免被系统检测
data = {}#data字典
data['type'] = 'AUTO'
data['i'] = count
data['doctype'] = 'json'
data['xmlVersion'] = '1.8'
data['keyfrom'] = 'fanyi.web'
data['ue'] = 'UTF-8'
#data['action'] = 'FY_BY_CLICKBUTTON'
data['typeResult'] = 'true'
data = urllib.parse.urlencode(data).encode('utf-8')#将这些字典转换为查询字符串之后以‘UTF-8’形式编码
req = urllib.request.Request(url,data,head)#URL请求的抽象
response = urllib.request.urlopen(req)#实例化这个抽象,返回网页内容
html = response.read().decode('utf-8')#以‘UTF-8’的编码格式解码
target = json.loads(html)#将json结构化的内容解包
print('翻译结果:%s' % (target['translateResult'][0][0]['tgt']))
time.sleep(2)
#隐藏head
|
评分
-
查看全部评分
|