【上海校区】Python爬取有道字典出现错误 {‘errorcode’:50}

python 未结 0 194
汽车网络营销
汽车网络营销 2021-05-28 09:31
悬赏:66
import urllib.request
import urllib.parse
import json
content = input("请输入带翻译的内容: ")
url = "http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
data = {}
data["i"]=content
data["from"]="AUTO"
data["to"]="AUTO"
data["smartresult"]= "dict"
data["client"]= "fanyideskweb"
data["salt"]="1535438497358"
data["sign"]= "f19e9cbeddb439ff1fcf17265010373e"
data["doctype"]= "json"
data["version"]= "2.1"
data["keyfrom"]= "fanyi.web"
data["action"]="FY_BY_CLICKBUTTION"
data["typoResult"]= "false"
data = urllib.parse.urlencode(data).encode("utf-8")

response = urllib.request.urlopen(url,data,)

html = response.read().decode("utf-8")

target = json.loads(html)
target = target["translateResult"][0][0]["tgt"]
print(target)
出现的错误是:

请输入带翻译的内容: i
Traceback (most recent call last):
  File "E:\python课后题\54-1.py", line 26, in <module>
    target = target["translateResult"][0][0]["tgt"]
KeyError: "translateResult"

将在审查元素中获得的url中translate后面的_o去掉,错误就消失
---------------------
作者:lgy54321
来源:CSDN
原文:https://blog.csdn.net/lgy54321/article/details/82184292
版权声明:本文为博主原创文章,转载请附上博文链接!

相关标签:
回答
  • 消灭零回复
提交回复