鱼C论坛

 找回密码
 立即注册
查看: 838|回复: 1

关于函数的调用

[复制链接]
发表于 2018-6-26 23:42:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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')
就没有问题了,上面的设定方法不对吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-7-27 17:32:31 | 显示全部楼层
没人啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-7-4 17:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表