小甲鱼python55讲,求大佬帮忙修改一下代码。
import urllib.requestimport urllib.parse
content = input("请输入需要翻译的内容:")
url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule&sessionFrom=null'
head = {}
head['User-Agent']= 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3756.400 QQBrowser/10.5.4039.400'
data = {}
date['i'] = 'content'
date['from']= 'AUTO'
date['to']= 'AUTO'
date['smartresult']= 'dict'
date['client']= 'fanyideskweb'
date['salt']= '15887282099161'
date['sign']= 'c44ee409735645b76958ecd9c031902f'
date['ts']='1588728209916'
date['bv']= 'b016bfc8dd420bcfc5d5a95c5a1600f4'
date['doctype']= 'json'
date['version']= '2.1'
date['keyfrom']= 'fanyi.web'
date['action']: 'FY_BY_CLICKBUTTIONdata'
response = urllib.request.urlopen(url,data)
html = response.read().decode('utf-8')
print(html)
出错:Traceback (most recent call last):
File "C:\Users\Milk\Desktop\python\1.py", line 11, in <module>
date['i'] = 'content'
NameError: name 'date' is not defined
求大佬教一下怎么修改,使程序可以运行。 本帖最后由 wuqramy 于 2020-5-6 09:51 编辑
这个
data = {}
不是date吗,改一下就OK
还有其他问题,见5楼 字典定义的是data,为啥变成date了? 字典名错了…… 1. 拼写错误,data 错写成了 date
2. 等于号写成了冒号
3. data 需要先转化为二进制才可以传给 urlopen
4. URL 需要去掉 _o 才可以正常爬取
代码帮你改好了:
import urllib.request
import urllib.parse
content = input("请输入需要翻译的内容:")
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&sessionFrom=null'
head = {}
head[
'User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) ' \
'Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3756.400 QQBrowser/10.5.4039.400'
data = {}
data['i'] = 'content'
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] = 'fanyideskweb'
data['salt'] = '15887282099161'
data['sign'] = 'c44ee409735645b76958ecd9c031902f'
data['ts'] = '1588728209916'
data['bv'] = 'b016bfc8dd420bcfc5d5a95c5a1600f4'
data['doctype'] = 'json'
data['version'] = '2.1'
data['keyfrom'] = 'fanyi.web'
data['action'] = 'FY_BY_CLICKBUTTIONdata'
response = urllib.request.urlopen(url, urllib.parse.urlencode(data).encode('utf-8'))
html = response.read().decode('utf-8')
print(html) wuqramy 发表于 2020-5-6 09:46
这个
不是date吗,改一下就OK
确定改一下就 OK?? zltzlt 发表于 2020-5-6 09:49
确定改一下就 OK??
哦!
没仔细看 zltzlt 发表于 2020-5-6 09:48
1. 拼写错误,data 错写成了 date
2. 等于号写成了冒号
3. data 需要先转化为二进制才可以传给 urlopen
谢谢大佬。 {:7_136:}
页:
[1]