python - "TypeError: object of type ' 响应 ' has no len()"

标签 python web-scraping beautifulsoup

当我尝试执行代码时

BeautifulSoup(html, ...)

它给出了错误信息

TypeError: object of type 'Response' has no len()

我尝试将实际的 HTML 作为参数传递,但它仍然不起作用。

import requests

url = 'http://vineoftheday.com/?order_by=rating'
response = requests.get(url)
html = response.content

soup = BeautifulSoup(html, "html.parser")

最佳答案

您正在获取 response.content。但它以字节(docs)的形式返回响应正文。但是您应该将 str 传递给 BeautifulSoup 构造函数 ( docs )。所以你需要使用 response.text 而不是获取内容。

关于python - "TypeError: object of type ' 响应 ' has no len()",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36709165/

相关文章:

python - 我可以从被调用函数内部区分位置参数和关键字参数吗?

python - 有没有办法从 SeLoger 中抓取广告的网址?

Python使用正则表达式查找文本中的所有内容

python - 如何使用 For 循环从 html 获取多个链接?

python - 如何访问特定 pdf 页面而不是整个文档的文本

python - 删除 NumPy 数组中的连续重复项

python - 将组上的顺序计数器列添加到 pandas 数据帧

web-scraping - 将输出(println!)转换为.csv文件

python - 使用 Beautifulsoup 和选择器检索内容

python - 使用 BeautifulSoup 在 HTML 表格中每个粗体标题后提取行中的特定列