python 3.5: a bytes-like object is required,not ‘str’ 报错

今天用python3的request写爬虫的时候遇到这个问题

python 3.5: a bytes-like object is required,not 'str' 报错
python 3.5: a bytes-like object is required,not ‘str’ 报错
python 3.5: a bytes-like object is required,not 'str' 报错
python 3.5: a bytes-like object is required,not ‘str’ 报错

查了很多,都没用,最后找到自己的解决办法
因为python3是bytes-like的,所以我们需要用split的话只能把它转为str

  # bytes object
  b = b"example"

  # str object
  s = "example"

  # str to bytes
  bytes(s, encoding = "utf8")

  # bytes to str
  str(b, encoding = "utf-8")

  # an alternative method
  # str to bytes
  str.encode(s)

  # bytes to str
  bytes.decode(b)

 

python 3.5: a bytes-like object is required,not 'str' 报错
python 3.5: a bytes-like object is required,not ‘str’ 报错

这样就可以顺利使用split了。

 

更多参考:

python:字符串string 开头r,b,u,f 含义, str bytes 转换 format, 字节转字符串

Python3: python3 内置函数, Python3 id() 详解, Python3 dir() 详解, Python3 str() 详解,Python3 type() 详解

 

本文:python 3.5: a bytes-like object is required,not ‘str’ 报错

Loading

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.