Python3中的字节,字符串和urllib

时间:2019-02-08 10:23:50

标签: python-3.x urllib

我无法解决这个错误。这确实很基础,但似乎与事实恰恰相反。

>>> x=b'hi'
>>> urllib.parse.unquote(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/parse.py", line 609, in unquote
    if '%' not in string:
TypeError: a bytes-like object is required, not 'str'

我已将代码传递给bytes对象,我希望它足够像字节一样。 urllib.parse.unquote()似乎仅适用于str对象。

那为什么为什么会生成一条错误消息,说它需要一个类似字节的对象?

1 个答案:

答案 0 :(得分:1)

我建议使用urllib.parse.unquote_to_bytes,如果接收到str对象,它将其字符串参数编码为字节。

get(complete("Hello, world"))