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

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

问题描述

问题:当我尝试执行脚本时, BeautifulSoup(html,...)给出错误消息TypeError:type of'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 。但是它以字节的形式返回响应体( str 传递给BeautifulSoup的构造函数( docs )所以你需要使用 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:类型'Response'的对象没有len()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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