求助:远程无响应,关闭连接怎么办?

求助:远程无响应,关闭连接怎么办?

我想用以下代码从网页获取HTML源代码:

import requests

url = "https://dictionary.cambridge.org/us/dictionary/english-arabic/hi"

r = requests.get(url)

但是得到了如下报错:

Traceback (most recent call last):

File "/home/username/ak_env/lib/python3.8/site-packages/urllib3/connectionpool.py", line 699, in urlopen

httplib_response = self._make_request(

File "/home/username/ak_env/lib/python3.8/site-packages/urllib3/connectionpool.py", line 445, in _make_request

six.raise_from(e, None)

File "<string>", line 3, in raise_from

File "/home/username/ak_env/lib/python3.8/site-packages/urllib3/connectionpool.py", line 440, in _make_request

httplib_response = conn.getresponse()

File "/usr/lib/python3.8/http/client.py", line 1347, in getresponse

response.begin()

File "/usr/lib/python3.8/http/client.py", line 307, in begin

version, status, reason = self._read_status()

File "/usr/lib/python3.8/http/client.py", line 276, in _read_status

raise RemoteDisconnected("Remote end closed connection without"

http.client.RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/home/username/ak_env/lib/python3.8/site-packages/requests/adapters.py", line 439, in send

resp = conn.urlopen(

File "/home/username/ak_env/lib/python3.8/site-packages/urllib3/connectionpool.py", line 755, in urlopen

retries = retries.increment(

File "/home/username/ak_env/lib/python3.8/site-packages/urllib3/util/retry.py", line 532, in increment

raise six.reraise(type(error), error, _stacktrace)

File "/home/username/ak_env/lib/python3.8/site-packages/urllib3/packages/six.py", line 734, in reraise

raise value.with_traceback(tb)

File "/home/username/ak_env/lib/python3.8/site-packages/urllib3/connectionpool.py", line 699, in urlopen

httplib_response = self._make_request(

File "/home/username/ak_env/lib/python3.8/site-packages/urllib3/connectionpool.py", line 445, in _make_request

six.raise_from(e, None)

File "<string>", line 3, in raise_from

File "/home/username/ak_env/lib/python3.8/site-packages/urllib3/connectionpool.py", line 440, in _make_request

httplib_response = conn.getresponse()

File "/usr/lib/python3.8/http/client.py", line 1347, in getresponse

response.begin()

File "/usr/lib/python3.8/http/client.py", line 307, in begin

version, status, reason = self._read_status()

File "/usr/lib/python3.8/http/client.py", line 276, in _read_status

raise 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 "/home/username/ak_env/lib/python3.8/site-packages/requests/api.py", line 76, in get

return request('get', url, params=params, **kwargs)

File "/home/username/ak_env/lib/python3.8/site-packages/requests/api.py", line 61, in request

return session.request(method=method, url=url, **kwargs)

File "/home/username/ak_env/lib/python3.8/site-packages/requests/sessions.py", line 542, in request

resp = self.send(prep, **send_kwargs)

File "/home/username/ak_env/lib/python3.8/site-packages/requests/sessions.py", line 655, in send

r = adapter.send(request, **kwargs)

File "/home/username/ak_env/lib/python3.8/site-packages/requests/adapters.py", line 498, in send

raise ConnectionError(err, request=request)

requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

大佬们,有没有办法解决这个问题?或者有其他函数可以获取源代码吗?我使用了wget,效果还不错。如果实在没有办法解决这个问题的话,我可能会用wget下载页面。


回答:

添加一个请求头就好了

h = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36'}

r = requests.get(url, headers=h)


本文参与了SegmentFault 思否面试闯关挑战赛,欢迎正在阅读的你也加入。

以上是 求助:远程无响应,关闭连接怎么办? 的全部内容, 来源链接: utcz.com/p/938539.html

回到顶部