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

本文探讨了在使用Python的BeautifulSoup和requests库时遇到的一个常见错误:TypeError: object of type 'Response' has no len()。作者尝试了将html作为参数传递给BeautifulSoup,但仍然无法解决问题。文中提供了具体的代码示例,包括请求URL、设置headers以及尝试解析HTML的代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

url = "https://www.ximalaya.com/shangye/6855034/"
    headers={
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:62.0) Gecko/20100101 Firefox/62.0',
    }
    html=requests.get(url,headers=headers)
    html.encoding = 'utf-8'
    # print(html.text)

    soup = BeautifulSoup(html.text, 'html.parser')
    # soup = BeautifulSoup(html, 'html.parser'),传入参数html不行哦
    print(soup.text)
https://stackoverflow.com/questions/36709165/beautifulsoup-object-of-type-response-has-no-len

 

在天津,上述代码块后打印到第六页的时候出现异常,ypeError Traceback (most recent call last) Cell In[51], line 35 33 # 传递正确URL到爬取函数 34 html = getHtmlDoc(current_url) ---> 35 page_comment = getComment(html) 36 all_comment.extend(page_comment) 37 time.sleep(3) # 延时3秒,防止被禁 Cell In[51], line 18, in getComment(html) 16 def getComment(html): 17 comment=[] ---> 18 soup = BeautifulSoup(html, 'html.parser')#使用 BeautifulSoup 库将传入的 HTML 文档解析成一个 soup 对象。 19 #这里指定使用 html.parser 作为解析器。 20 #soup = BeautifulSoup(html, 'lxml')#使用 lxml 库解析 HTML 文档,速度更快。 21 comment = soup.find_all("p", class_="comment-content") File ~\anaconda3\Lib\site-packages\bs4\__init__.py:315, in BeautifulSoup.__init__(self, markup, features, builder, parse_only, from_encoding, exclude_encodings, element_classes, **kwargs) 313 if hasattr(markup, 'read'): # It's a file-type object. 314 markup = markup.read() --> 315 elif len(markup) <= 256 and ( 316 (isinstance(markup, bytes) and not b'<' in markup) 317 or (isinstance(markup, str) and not '<' in markup) 318 ): 319 # Issue warnings for a couple beginner problems 320 # involving passing non-markup to Beautiful Soup. 321 # Beautiful Soup will still parse the input as markup, 322 # since that is sometimes the intended behavior. 323 if not self._markup_is_url(markup): 324 self._markup_resembles_filename(markup) TypeError: object of type 'NoneType' has no len()
最新发布
03-28
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值