爬取有道翻译

看小甲鱼的视频发现老方法不管用了,贴上根据视频方法写的代码

#-*-coding:utf-8-*-
'''
本例试着利用网上有道翻译进行本地翻译
--要求提示输入翻译的中文“请输入翻译中文”
--把输入的中文翻译成英文
使用资源json urllib.request urllib.parse
'''
import urllib.request
import urllib.parse
import json

url1='http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'

head={}
head['User-Agent']=r'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'

data={}
data['i']='小甲鱼是帅哥'
data['from']='AUTO'
data['to']='AUTO'
data['smartresult']='dict'
data['client']='fanyideskweb'
data['salt']='15751737837827'
data['sign']='2987b1611055996ce0501fd8a526aad'
data['ts']='1575173783782'
data['bv']='710f3e24cb0088b9d9ea448919deb3bb'
data['doctype']='json'
data['version']='2.1'
data['keyfrom']='fanyi.web'
data['action']='FY_BY_REALTlME'
data = urllib.parse.urlencode(data).encode('utf-8')

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

print(html)

发现报错误{"errorCode":50}

到网上一搜发现有道变了,根据网上的提示将url中的_o去掉,不知道原理
{"type":"ZH_CN2EN","errorCode":0,"elapsedTime":1,"translateResult":[[{"src":"小甲鱼是帅哥","tgt":"The little turtle is a handsome boy"}]]}

你可能感兴趣的:(爬取有道翻译)