qq_38687954
泠然79
2019-01-23 16:36

JSONDecodeError爬虫有道翻译

  • python

新人求问啊啊啊啊啊~~~~下面是我的代码

import urllib.request
import urllib.parse
import json

content=input("请输入需要翻译内容:")
url="http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"#?前面有个_o去掉才能正常使用 why?
data={}
data["i"]: content
data["type"]: "AUTO"
data["smartresult"]: "dict"
data["client"]: "fanyideskweb"
data["salt"]: "15482158645682"
data["sign"]: "91cbd7628c2de9065132acf3b88da8cd"
data["doctype"]: "json"
data["version"]: "2.1"
data["keyfrom"]: "fanyi.web"
data["action"]: "FY_BY_REALTIME"
data["typoResult"]: "true"

data=urllib.parse.urlencode(data).encode("utf-8")
response=urllib.request.urlopen(url,data)
html=response.read().decode("utf-8")
target=json.loads(html)
print("翻译结果是:%s"%(target["translateResult"][0][0]["tgt"]))


运行后的结果是
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\1.23.3.py", line 23, in
target=json.loads(html)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\json__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
不知道该怎么改 求问大神

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐

换一换