【问题标题】:TypeError: object of type 'Response' has no len() [BeautifulSoup]TypeError:“响应”类型的对象没有 len() [BeautifulSoup]
【发布时间】:2017-08-07 20:04:55
【问题描述】:

我正在尝试登录我的大学网站,然后从中删除数据,(暂时只是欢迎页面的源代码)但我得到了以下回溯..

Traceback (most recent call last):
   File "C:/Users/HUNTER/PycharmProjects/Encryption/erp.py", line 16, in   <module>
   soup = BeautifulSoup(opens, 'lxml')
   File "C:\Users\HUNTER\AppData\Local\Programs\Python\Python35-32\lib\site-packages\bs4\__init__.py", line 192, in __init__
   elif len(markup) <= 256 and (
   TypeError: object of type 'Response' has no len()  

这里是代码..

 import requests
 from requests import session
 from bs4 import BeautifulSoup

 url = 'http://erp.college_name.edu/'

 r = requests.session()
 data = {

    'tbUserName': 'username',
    'tbPassword': 'password'

 } 

opens = r.post(url=url, data=data)
soup = BeautifulSoup(opens, 'lxml')
print(soup)

帮助真的很重要..

更新

所以,做,
soup = BeautifulSoup(opens.text, 'lxml')
print(soup)

给了我这个..

  C:\Users\HUNTER\AppData\Local\Programs\Python\Python35-32\python.exe     
  C:/Users/HUNTER/PycharmProjects/Encryption/erp.py
  Traceback (most recent call last):
   File "C:\Users\HUNTER\AppData\Local\Programs\Python\Python35-32\lib\site-  
     packages\requests\packages\urllib3\response.py", line 232, in    
    _error_catcher
     yield
    File "C:\Users\HUNTER\AppData\Local\Programs\Python\Python35-   
   32\lib\site-packages\requests\packages\urllib3\response.py", line 314, in  
   read
   data = self._fp.read(amt)
    File "C:\Users\HUNTER\AppData\Local\Programs\Python\Python35- 
   32\lib\http\client.py", line 448, in read
    n = self.readinto(b)
    File "C:\Users\HUNTER\AppData\Local\Programs\Python\Python35-
  32\lib\http\client.py", line 488, in readinto
     n = self.fp.readinto(b)
   File "C:\Users\HUNTER\AppData\Local\Programs\Python\Python35-
  32\lib\socket.py", line 575, in readinto
   return self._sock.recv_into(b)

   ConnectionResetError: [WinError 10054] An existing connection was    
   forcibly closed by the remote host


   During handling of the above exception, another exception occurred:

      Traceback (most recent call last):
        File "C:\Users\HUNTER\AppData\Local\Programs\Python\Python35-    
      32\lib\site-packages\requests\models.py", line 676, in generate
     for chunk in self.raw.stream(chunk_size, decode_content=True):
        File "C:\Users\HUNTER\AppData\Local\Programs\Python\Python35-  
      32\lib\site-packages\requests\packages\urllib3\response.py", line 357,   
  in stream
    data = self.read(amt=amt, decode_content=decode_content)
        File "C:\Users\HUNTER\AppData\Local\Programs\Python\Python35-  

        32\lib\site-packages\requests\packages\urllib3\response.py", line  
      324, in read
   flush_decoder = True
        File "C:\Users\HUNTER\AppData\Local\Programs\Python\Python35- 
      32\lib\contextlib.py", line 77, in __exit__
self.gen.throw(type, value, traceback)
       File "C:\Users\HUNTER\AppData\Local\Programs\Python\Python35-     
   32\lib\site-   
   packages\requests\packages\urllib3\response.py", line 250, in   
   _error_catcher
     raise ProtocolError('Connection broken: %r' % e, e)
    requests.packages.urllib3.exceptions.ProtocolError: ("Connection broken:    
ConnectionResetError(10054, 'An existing connection was forcibly closed by    
   the remote host', None, 10054, None)", ConnectionResetError(10054, 'An   
    existing connection was forcibly closed by the remote host', None,    
  10054, None))

  During handling of the above exception, another exception occurred:

   Traceback (most recent call last):
     File "C:/Users/HUNTER/PycharmProjects/Encryption/erp.py", line 15, in <module>
opens = r.post(url=url, data=data)
    File "C:\Users\HUNTER\AppData\Local\Programs\Python\Python35-
  32\lib\site-packages\requests\sessions.py", line 522, in post
return self.request('POST', url, data=data, json=json, **kwargs)
    File "C:\Users\HUNTER\AppData\Local\Programs\Python\Python35-
 32\lib\site-packages\requests\sessions.py", line 475, in request
resp = self.send(prep, **send_kwargs)
     File "C:\Users\HUNTER\AppData\Local\Programs\Python\Python35-
  32\lib\site-packages\requests\sessions.py", line 628, in send
r.content
    File "C:\Users\HUNTER\AppData\Local\Programs\Python\Python35-
    32\lib\site-packages\requests\models.py", line 755, in content
self._content = bytes().join(self.iter_content(CONTENT_CHUNK_SIZE)) or 

bytes()
  File "C:\Users\HUNTER\AppData\Local\Programs\Python\Python35-32\lib\site-
     packages\requests\models.py", line 679, in generate
raise ChunkedEncodingError(e)

    requests.exceptions.ChunkedEncodingError: ("Connection broken: 

ConnectionResetError(10054, 'An existing connection was forcibly closed by    

 the    
 remote host', None, 10054, None)", ConnectionResetError(10054, 'An existing   
 connection was forcibly closed by the remote host', None, 10054, None))

   Process finished with exit code 1

【问题讨论】:

    标签: python python-3.x web-scraping beautifulsoup python-requests


    【解决方案1】:

    BeautifulSoup 需要返回的内容,而不是完整的响应。尝试使用opens.text

    opens = r.post(url=url, data=data)
    soup = BeautifulSoup(opens.text, 'lxml')
    print(soup)
    

    【讨论】:

      【解决方案2】:

      我之前遇到过这个错误,我有答案。 导致该错误的原因有很多。 这可能是由于连接的两端在保持连接期间连接是否超时的问题上存在分歧。

      要详细检查问题,可以使用 Wireshark。

      或者您可以重新请求或重新连接。

      【讨论】:

        猜你喜欢
        • 2016-08-11
        • 2020-04-18
        • 1970-01-01
        • 1970-01-01
        • 2015-08-21
        • 2019-05-23
        • 2013-04-18
        • 2015-01-21
        • 2021-12-19
        相关资源
        最近更新 更多