我给你 来一个 我写的吧 你看看 和你的区别 你返回的不是Json应该是post数据有问题import urllib.request
import urllib.parse
import json
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
proxy_url = ['183.146.213.157:80','101.231.104.82','39.137.69.10:80','101.95.115.196:80']#代理列表
index = 0
while True:
inputStr = input('\n请输入你想要翻译的中文(输入q退出):\n')
while True:
try:
if inputStr == 'q':
break
post = {'i':inputStr,'from': 'zh-CHS','to':' en','smartresult':' dict','client': 'fanyideskweb',
'salt': '15766316706921','sign': '226a73a4714da07f6fad23fa645c2240','ts': '1576631670692',
'bv': '5575008ba9785f184b106838a72d6536','doctype':'json','version': '2.1','keyfrom': 'fanyi.web',
'action': 'FY_BY_REALTlME'}
postRealData = urllib.parse.urlencode(post).encode('utf-8')
#绑定代理
proxy = urllib.request.ProxyHandler({'http':proxy_url[index]})#绑定代理网址的程序 协议:网址端口 字典
opener = urllib.request.build_opener(proxy)#理解为 代理服务可以运用
urllib.request.install_opener(opener)#默认的全局代理服务(其实可以定义多个代理,这个是不设置默认的)
req = urllib.request.Request(url,data = postRealData)
#opener没有添加报头的函数
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36')#伪装浏览器访问
response = urllib.request.urlopen(req)
responseData = response.read().decode('utf-8')
jsonData = json.loads(responseData)
translateResult = jsonData['translateResult'][0][0]['tgt']
print('翻译的结果是' + translateResult)
break
except Exception:
index =index + 1
index = index % len(proxy_url)
index =index + 1
index = index % len(proxy_url)
|