Python:requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine("''",))

Posted

技术标签:

【中文标题】Python:requests.exceptions.ConnectionError: (\'Connection aborted.\', BadStatusLine("\'\'",))【英文标题】:Python:requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine("''",))Python:requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine("''",)) 【发布时间】:2015-11-21 21:25:42 【问题描述】:

当我尝试从网站下载大量页面时遇到此错误。脚本是由其他几个脚本拼凑和修改的,看来我对Python和编程比较陌生。

Python的版本是3.4.3,Requests的版本是2.7.0。

这是脚本:

import requests
from bs4 import BeautifulSoup
import os.path

s = requests.session()
login_data = 'dest': '/','user': '******', 'pass': '******'
header_info='User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0'
url='http://www.oxfordreference.com/LOGIN'
s.post(url,data=login_data,headers=header_info)

for i in range(1,100):
    downprefix='http://www.oxfordreference.com/view/10.1093/acref/9780198294818.001.0001/acref-9780198294818-e-'
    downurl=downprefix+str(i)
    r=s.get(downurl,headers=header_info,timeout=30)
    if r.status_code==200:
        soup=BeautifulSoup(r.content,"html.parser")
        shorten=str(soup.find_all("div", class_="entryContent"))
        fname='acref-9780198294818-e-'+str(i)+'.htm'
        newname=os.path.join('shorten',fname)
        htmfile=open(newname,'w',encoding="utf_8")
        htmfile.write(shorten)
        htmfile.close()
        print('Success in '+str(i))
else:
        print('Error in '+str(i))
        errorfile=open('errors.txt','a',encoding="utf_8")
        errorfile.write(str(i))
        errorfile.write('\n')
        errorfile.close()

完整的引用是:

Traceback (most recent call last):
  File "D:\Program Files (x86)\python343\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 372, in _make_request
    httplib_response = conn.getresponse(buffering=True)
TypeError: getresponse() got an unexpected keyword argument 'buffering'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Program Files (x86)\python343\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 544, in urlopen
    body=body, headers=headers)
  File "D:\Program Files (x86)\python343\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 374, in _make_request
    httplib_response = conn.getresponse()
  File "D:\Program Files (x86)\python343\lib\http\client.py", line 1171, in getresponse
    response.begin()
  File "D:\Program Files (x86)\python343\lib\http\client.py", line 351, in begin
    version, status, reason = self._read_status()
  File "D:\Program Files (x86)\python343\lib\http\client.py", line 321, in _read_status
    raise BadStatusLine(line)
http.client.BadStatusLine: ''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Program Files (x86)\python343\lib\site-packages\requests\adapters.py", line 370, in send
    timeout=timeout
  File "D:\Program Files (x86)\python343\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 597, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "D:\Program Files (x86)\python343\lib\site-packages\requests\packages\urllib3\util\retry.py", line 245, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "D:\Program Files (x86)\python343\lib\site-packages\requests\packages\urllib3\packages\six.py", line 309, in reraise
    raise value.with_traceback(tb)
  File "D:\Program Files (x86)\python343\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 544, in urlopen
    body=body, headers=headers)
  File "D:\Program Files (x86)\python343\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 374, in _make_request
    httplib_response = conn.getresponse()
  File "D:\Program Files (x86)\python343\lib\http\client.py", line 1171, in getresponse
    response.begin()
  File "D:\Program Files (x86)\python343\lib\http\client.py", line 351, in begin
    version, status, reason = self._read_status()
  File "D:\Program Files (x86)\python343\lib\http\client.py", line 321, in _read_status
    raise BadStatusLine(line)
requests.packages.urllib3.exceptions.ProtocolError: ('Connection aborted.', BadStatusLine("''",))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\stuff\Mdict\dict by me\odoa\newahktest\CrawlTest2.py", line 14, in <module>
    r=s.get(downurl,headers=header_info,timeout=30) 
  File "D:\Program Files (x86)\python343\lib\site-packages\requests\sessions.py", line 477, in get
    return self.request('GET', url, **kwargs)
  File "D:\Program Files (x86)\python343\lib\site-packages\requests\sessions.py", line 465, in request
    resp = self.send(prep, **send_kwargs)
  File "D:\Program Files (x86)\python343\lib\site-packages\requests\sessions.py", line 573, in send
    r = adapter.send(request, **kwargs)
  File "D:\Program Files (x86)\python343\lib\site-packages\requests\adapters.py", line 415, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine("''",))

【问题讨论】:

【参考方案1】:

您正在与之交谈的主持人没有正确响应。这通常发生在您尝试使用 http 连接到 https 服务时,但也可能有很多其他情况。

可能最好的检查方式是获取网络流量分析器(例如wireshark)并查看连接。

【讨论】:

感谢您的建议。我彻底查看了 Firefox 开发者工具中的请求标头,发现浏览器中的 cookie 与我在“r=s.get(downurl,headers=header_info,timeout=30)”之后得到的 cookie 不同。有什么问题吗?

以上是关于Python:requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine("''",))的主要内容,如果未能解决你的问题,请参考以下文章