python爬取有道词典json报错,求帮助!

python爬取有道词典json报错,求帮助!

import urllib.request
import urllib.parse
import json
import time
import random
import hashlib

fanyi = input("输入你想翻译的文字:")

url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
head = {}
head['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36'

u = 'fanyideskweb'
d = fanyi
f = str(int(time.time() * 1000) + random.randint(1, 10))
c = 'rY0D^0\'nM0}g5Mm1z%1G4'

sign = hashlib.md5((u + d + f + c).encode('utf-8')).hexdigest()
data = {}
data['i'] = fanyi
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] = 'fanyideskweb'
data['salt'] = f  #'15503716490563'
data['sign'] = sign  # 'e1ad61ef4d013802bc9117ee6ee1a053'
data['ts'] = '1550371649056'
data['bv'] = '617939f69fb18f112aa988d6038ae43f'
data['json'] = 'doctype'
data['vison'] = 'version'
data['keyfrom'] = 'fanyi.web'
data['action'] = 'FY_BY_REALTIME'
data['typoResult'] = 'false'

data = urllib.parse.urlencode(data).encode('utf-8')
response = urllib.request.Request(url, data)
response.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36')
req = urllib.request.urlopen(response)

html = req.read().decode('utf-8')

print(html)
target = json.loads(html)
result = target['translateResult'][0][0]['tgt']

print(result)

运行程序输出的html为:



    
    


    
    
    在线翻译_有道
    
    
    
    
    
    
           
    
    
    

    
    
上传文档
全新中英文档互译 ×
全新文档翻译,支持docx /pdf 等格式中英互译,快来试用!
0/5000
朗读
选择翻译语言,然后点击翻译按钮,即可翻译文档
您提供的翻译将用于改善翻译质量,感谢您的建议!
来自有道词典结果
美丽 [měi lì]
查看完整结果>>
点击可查看其他翻译结果,或修改结果
以下为该句多个翻译结果:
扫描二维码
关注有道人工翻译
人工
翻译
请在IE8以上版本,或Chrome、火狐、Safari等浏览器中访问该网页。
解析中
取消
如需使用文档翻译功能,请先登录。

提示报错:
Traceback (most recent call last):
File “C:/Users/luojiabao.JFFAW/PycharmProjects/untitled/youdaofanyi.py”, line 46, in
target = json.loads(html)
File “D:\Python\lib\json_init_.py”, line 348, in loads
return _default_decoder.decode(s)
File “D:\Python\lib\json\decoder.py”, line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File “D:\Python\lib\json\decoder.py”, line 355, in raw_decode
raise JSONDecodeError(“Expecting value”, s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

你可能感兴趣的:(python爬取有道词典json报错,求帮助!)