近期遇到一个问题使用python requests.get发送http请求,有些url直接报错,返回requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response')),但是浏览器却可以正常打开网页,这是怎么回事呢?
报错信息详情如下:
r=requests.get("http://www.xxz.gov.cn/2018zhuanti/jczwgkzl/bmfl.html?Parent=14")Traceback (most recent call last):File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopenhttplib_response = self._make_request(File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 449, in _make_requestsix.raise_from(e, None)File "<string>", line 3, in raise_fromFile "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 444, in _make_requesthttplib_response = conn.getresponse()File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1347, in getresponseresponse.begin()File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 307, in beginversion, status, reason = self._read_status()File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 276, in _read_statusraise RemoteDisconnected("Remote end closed connection without"http.client.RemoteDisconnected: Remote end closed connection without responseDuring handling of the above exception, another exception occurred:Traceback (most recent call last):File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\adapters.py", line 440, in sendresp = conn.urlopen(File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 785, in urlopenretries = retries.increment(File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\util\retry.py", line 550, in incrementraise six.reraise(type(error), error, _stacktrace)File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\packages\six.py", line 769, in reraiseraise value.with_traceback(tb)File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopenhttplib_response = self._make_request(File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 449, in _make_requestsix.raise_from(e, None)File "<string>", line 3, in raise_fromFile "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 444, in _make_requesthttplib_response = conn.getresponse()File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1347, in getresponseresponse.begin()File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 307, in beginversion, status, reason = self._read_status()File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 276, in _read_statusraise RemoteDisconnected("Remote end closed connection without"urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))During handling of the above exception, another exception occurred:Traceback (most recent call last):File "<stdin>", line 1, in <module>File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\api.py", line 75, in getreturn request('get', url, params=params, **kwargs)File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\api.py", line 61, in requestreturn session.request(method=method, url=url, **kwargs)File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 529, in requestresp = self.send(prep, **send_kwargs)File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 645, in sendr = adapter.send(request, **kwargs)File "C:\Users\zhongxin\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\adapters.py", line 501, in sendraise ConnectionError(err, request=request)requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
遇到这种情况,最先需要考虑请求头User-Agent,User-Agent是一个字符串,它描述了发送请求的客户端。如果服务器检测到User-Agent不是浏览器,它可能会关闭连接。默认情况下requests的User-Agent为'python-requests/2.27.1',既然浏览器可以访问,那么就可以尝试通过修改User-Agent解决这个问题。
>>> r=requests.get("http://www.xxz.gov.cn/2018zhuanti/jczwgkzl/bmfl.html?Parent=14",headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.41"})>>> r<Response [200]>>>> r.request.headers{'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.41', 'Accept-Encoding': 'gzip, deflate, br', 'Accept': '*/*', 'Connection': 'keep-alive'}
文章转载自一点鑫得,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




