requests.get()在获取数据时发出错误

时间:2018-02-15 10:58:26

标签: python python-3.x python-requests

我还在学习python的阶段。所以,这就是我想做的事情。我从accuweather页面获取数据以显示当前的天气。但是请求模块似乎给出了某种错误。

> import requests
> from bs4 import *
> data = requests.get("https://www.accuweather.com/en/us/new-york-ny/10017/weather-forecast/349727")

Traceback (most recent call last):
  File "/home/nishantsikri/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 601, in urlopen
chunked=chunked)
  File "/home/nishantsikri/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 387, in _make_request
six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "/home/nishantsikri/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 383, in _make_request
httplib_response = conn.getresponse()
  File "/usr/lib/python3.6/http/client.py", line 1331, in getresponse
response.begin()
  File "/usr/lib/python3.6/http/client.py", line 297, in begin
version, status, reason = self._read_status()
  File "/usr/lib/python3.6/http/client.py", line 258, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/usr/lib/python3.6/socket.py", line 586, in readinto
return self._sock.recv_into(b)
  File "/usr/lib/python3.6/ssl.py", line 1009, in recv_into
return self.read(nbytes, buffer)
  File "/usr/lib/python3.6/ssl.py", line 871, in read
return self._sslobj.read(len, buffer)
  File "/usr/lib/python3.6/ssl.py", line 631, in read
    v = self._sslobj.read(len, buffer)
ConnectionResetError: [Errno 104] Connection reset by peer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/nishantsikri/.local/lib/python3.6/site-packages/requests/adapters.py", line 440, in send
timeout=timeout
  File "/home/nishantsikri/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 639, in urlopen
_stacktrace=sys.exc_info()[2])
  File "/home/nishantsikri/.local/lib/python3.6/site-packages/urllib3/util/retry.py", line 357, in increment
raise six.reraise(type(error), error, _stacktrace)
  File "/home/nishantsikri/.local/lib/python3.6/site-packages/urllib3/packages/six.py", line 685, in reraise
raise value.with_traceback(tb)
  File "/home/nishantsikri/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 601, in urlopen
chunked=chunked)
  File "/home/nishantsikri/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 387, in _make_request
six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "/home/nishantsikri/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 383, in _make_request
httplib_response = conn.getresponse()
  File "/usr/lib/python3.6/http/client.py", line 1331, in getresponse
response.begin()
  File "/usr/lib/python3.6/http/client.py", line 297, in begin
version, status, reason = self._read_status()
  File "/usr/lib/python3.6/http/client.py", line 258, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/usr/lib/python3.6/socket.py", line 586, in readinto
return self._sock.recv_into(b)
  File "/usr/lib/python3.6/ssl.py", line 1009, in recv_into
return self.read(nbytes, buffer)
  File "/usr/lib/python3.6/ssl.py", line 871, in read
return self._sslobj.read(len, buffer)
  File "/usr/lib/python3.6/ssl.py", line 631, in read
    v = self._sslobj.read(len, buffer)
urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
data = requests.get("https://www.accuweather.com/en/us/new-york-ny/10017/weather-forecast/349727")
  File "/home/nishantsikri/.local/lib/python3.6/site-packages/requests/api.py", line 72, in get
return request('get', url, params=params, **kwargs)
  File "/home/nishantsikri/.local/lib/python3.6/site-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
  File "/home/nishantsikri/.local/lib/python3.6/site-packages/requests/sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
  File "/home/nishantsikri/.local/lib/python3.6/site-packages/requests/sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
  File "/home/nishantsikri/.local/lib/python3.6/site-packages/requests/adapters.py", line 490, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))

1 个答案:

答案 0 :(得分:1)

某些网站更改其HTTP响应代码或内容,或使用requests的默认User-Agent标头删除HTTP请求的连接。

在这种情况下,您所要做的就是更改headers中的User-Agent字符串,例如

import requests

url = "https://www.accuweather.com/en/us/new-york-ny/10017/weather-forecast/349727"
data = requests.get(url, headers={'User-Agent':'my agent'})
print(data)
<Response [200]>