【问题标题】:TypeError: a bytes-like object is required, not 'str' in python 3.5TypeError:需要一个类似字节的对象,而不是 python 3.5 中的“str”
【发布时间】:2018-06-01 00:37:54
【问题描述】:

我正在学习 python 3.5 中的数据抓取并试图在网站上找到一个单词的存在,我收到错误:

TypeError: 需要一个类似字节的对象,而不是 'str'

 pyladies_source = uReq("http://pyladies.com")
 pyladies_source = pyladies_source.read()
'python' in pyladies_source

【问题讨论】:

  • 听起来pyladies_source的类型是字节
  • 你试过str()吗?
  • 你能告诉我解决方案的代码吗,我对python不太精通
  • 以后,当您对导入使用自定义命名时,请提供您的导入。例如,表明您使用了from urllib.request import urlopen as uReq

标签: python python-3.x web-scraping


【解决方案1】:
pyladies_source = uReq("http://pyladies.com")
pyladies_source = pyladies_source.read()
'python' in str(pyladies_source) # Returns false in this case, at least when I ran it.

您所要做的只是将pyladies_source 转换为字符串,这样您就可以查找'python'

【讨论】:

    猜你喜欢
    • 2017-08-31
    • 1970-01-01
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    • 2016-01-05
    相关资源
    最近更新 更多