54课urllib.parse.urlencode(Data).encode('utf-8')编码问题
代码如下:import urllib.request
import urllib.parse
content=input('请输入你要翻译的内容')
url='https://fanyi.baidu.com/v2transapi?from=zh&to=en'
Data={}
Data['from']='zh'
Data['to']='en'
Data['query']=content
Data['transtype']='translang'
Data['simple_means_flag']='3'
Data['sign']='144005.446900'
Data['token']='60eadd34ff0bd942344ada80100c8e29'
Data['domain']='common'
Data=urllib.parse.urlencode(Data).encode('utf-8')
response=urllib.request.urlopen(url.Data)
html=response.read().decode()
print(html)
在输入翻译内容后会报错
Traceback (most recent call last):
File "D:/pythonProject/爬虫/爬虫.py", line 17, in <module>
response=urllib.request.urlopen(url.Data)
AttributeError: 'str' object has no attribute 'Data'
使用type(Data)后
显示Data是bytes型
type(Data)
Out: bytes
请问如何解决? response=urllib.request.urlopen(url,Data)
这里应该是逗号把,不是点
貌似这样查不出
{"errno":997,"errmsg":"\u672a\u77e5\u9519\u8bef","query":"\u732a","from":"zh","to":"en","error":997} 应该是Data出了问题,因为使用的浏览器不同,没办法细说,我的是火狐,代码如下
import urllib.request
import urllib.parse
import json
while True:
temp = input("请输入要翻译的文本")
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
data = {}
data['i'] =temp
data['client'] = 'fanyideskweb'
data['salt'] = '15933351272349'
data['sign'] = 'a356b81cb4cdfcc7d308e573bc271fcc'
data['ts'] = '1593335127234'
data['bv'] = '043c9b8297522e9a37a779af14b3350c'
data['doctype'] = 'json'
data['version'] ='2.1'
data['keyfrom'] = 'fanyi.web'
data['action'] ='FY_BY_REALTlME'
data = urllib.parse.urlencode(data).encode('utf-8')
response = urllib.request.urlopen(url,data)
html = response.read().decode('utf-8')
result = json.loads(html)
print(result['translateResult']['tgt'])
笔记本看的眼花点和逗号都没看出来{:10_250:}{:10_266:}{:10_266:}{:10_266:} 小甲鱼的铁粉 发表于 2020-9-19 17:24
应该是Data出了问题,因为使用的浏览器不同,没办法细说,我的是火狐,代码如下
你这个能翻译出来 我这百度翻译没用{:10_266:}
页:
[1]