马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
[b]urllib.parse.urlencode(query, doseq=False, safe='', encoding=None, errors=None, quote_via=quote_plus) [/b]
Convert a mapping object or a sequence of two-element tuples, which may contain str or bytes objects, to a percent-encoded ASCII text string. If the resultant string is to be used as a data for POST operation with the urlopen() function, then it should be encoded to bytes, otherwise it would result in a TypeError.
关于urllib.parse.urlencode(query, doseq=False, safe='', encoding=None, errors=None, quote_via=quote_plus) 函数,我想对encoding 进行设定,
data=urllib.parse.urlencode(data,encoding='UTF-8')
结果报错:Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\爬虫\2.有道词典\translation.py", line 24, in <module>
response=urllib.request.urlopen(url,data)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 524, in open
req = meth(req)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 1248, in do_request_
raise TypeError(msg)
TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.
代码更改后:
data=urllib.parse.urlencode(data).encode=('UTF-8')
就没有问题了,上面的设定方法不对吗? |