Nelson盟新 发表于 2020-3-20 22:15:39

求大神解救,怎么改有道爬虫,AttributeError: 'str' object has no attribute 'read'

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['type']='AUTO'
data['smartresult']='dict'
data['client']='fanyideskweb'
data['salt']='15847082553867'
data['sign']='3eaa7ef338cd6d4ab1a22795d6b9a12d'
data['ts']='1584708255386'
data['bv']= 'ec579abcd509567b8d56407a80835950'
data['doctype']= 'json'
data['version']= '2.1'
data['keyfrom']= 'fanyi.web'
data['action']= 'FY_BY_CLICKBUTTION'
data['ue']='UTF-8'
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.load(html)

print("翻译结果:%s"% (target['translateResult']['tgt']))

suchocolate 发表于 2020-3-21 09:28:55

1.urlopen没有正确加data,应该这样:
response = urllib.request.urlopen(url, data = data)
2.另外如果要查词典,得加headers,不然网站反扒就拒绝你的查询了:
headers = {'user-agent': 'firefox'}
req = urllib.request.Request(url, data = data, headers = headers)
response = urllib.request.urlopen(req)
3.再有,翻译的url需要改成如下,去掉_o,这是网易设置的反扒:
http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule
4.你可以参考我之前做的案例:
https://fishc.com.cn/thread-155634-1-1.html
页: [1]
查看完整版本: 求大神解救,怎么改有道爬虫,AttributeError: 'str' object has no attribute 'read'