BeautifulSoup:“响应"类型的对象没有 len() [英] BeautifulSoup: object of type 'Response' has no len()

查看:32
本文介绍了BeautifulSoup:“响应"类型的对象没有 len()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:当我尝试执行脚本时,BeautifulSoup(html, ...) 给出错误消息TypeError:'Response' 类型的对象没有 len().我尝试通过实际的 html 作为参数,但它仍然不起作用.

Issue: when I try to execute the script, BeautifulSoup(html, ...) gives the error message "TypeError: object of type 'Response' has no len(). I tried passing the actual html as a parameter, but it still doesn't work.

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 构造函数(文档).所以你需要使用 response.text 而不是获取内容.

You are getting response.content. But it return response body as bytes (docs). But you should pass str to BeautifulSoup constructor (docs). So you need to use the response.text instead of getting content.

这篇关于BeautifulSoup:“响应"类型的对象没有 len()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆