BeautifulSoup:'Response'类型的对象没有len()

Bry*_*yan 20 html python parsing beautifulsoup web-scraping

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

import requests

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

soup = BeautifulSoup(html, "html.parser")
Run Code Online (Sandbox Code Playgroud)

小智 27

你来了response.content.但它将响应体返回为字节(docs).但是你应该传递str给BeautifulSoup构造函数(docs).所以你需要使用response.text而不是获取内容.


Jor*_*rge 19

尝试直接传递HTML文本

soup = BeautifulSoup(html.text)
Run Code Online (Sandbox Code Playgroud)


Mos*_*e G 9

如果您要使用requests.get('https://example.com')来获取 HTML,则应该使用requests.get('https://example.com').text.


小智 6

html.parser 用于忽略页面中的警告:

soup = BeautifulSoup(html.text, "html.parser")
Run Code Online (Sandbox Code Playgroud)