【问题标题】:BeautifulSoup: object of type 'Response' has no len()BeautifulSoup:“响应”类型的对象没有 len()
【发布时间】:2016-08-11 02:05:49
【问题描述】:

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

import requests

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

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

【问题讨论】:

标签: python html parsing web-scraping beautifulsoup


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    尝试直接传递 HTML 文本

    soup = BeautifulSoup(html.text)
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

          您在“响应”中仅获得响应代码 并始终使用浏览器标头以确保安全 你会遇到很多问题

          在调试器控制台网络部分 'header' UserAgent 中查找标头

          试试

          import requests
          from bs4 import BeautifulSoup
          
          from fake_useragent import UserAgent
          
          url = 'http://www.google.com'
          headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) 
          AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}
          
          response = requests.get(quote_page, headers=headers).text
          
          soup = BeautifulSoup(response, 'html.parser')
          print(soup.prettify())
          

          【讨论】:

            【解决方案6】:

            它对我有用:

            soup = BeautifulSoup(requests.get("your_url").text)
            

            现在,下面的代码更好(使用 lxml 解析器):

            import requests
            from bs4 import BeautifulSoup
            
            soup = BeautifulSoup(requests.get("your_url").text, 'lxml')
            

            【讨论】:

              【解决方案7】:

              您应该使用 .text 来获取响应内容

              import  requests
              url = 'http://www ... '
              response = requests.get(url)
              print(response.text)
              

              或与肥皂

              一起使用
              import  requests
              from bs4 import BeautifulSoup
              
              url = 'http://www ... '
              response = requests.get(url)
              msg = response.text
              print(BeautifulSoup(msg,'html.parser'))
              

              【讨论】:

                【解决方案8】:
                import requests
                from urllib.request import urlopen
                from bs4 import BeautifulSoup
                import re
                
                url = "https://fortnitetracker.com/profile/all/DakshRungta123"
                html = requests.get(url)
                
                soup = BeautifulSoup(html)
                
                
                title = soup.text
                print(title.text)
                

                【讨论】:

                • 欢迎来到 StackOverflow!请提供一些关于您的解决方案的解释
                猜你喜欢
                • 2017-08-07
                • 2020-04-18
                • 1970-01-01
                • 1970-01-01
                • 2015-08-21
                • 2013-03-14
                • 2022-01-08
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多