代码没问题,但是每次运行跑到一半就出错,报timeout是怎么回事啊

代码没问题,都是跟着老师的讲解编写的,比如调用接口或者爬虫,每次跑到一半就出错,错误信息:
Traceback (most recent call last):
  File "D:\Program Files\JetBrains\project\weather\venv\lib\site-packages\urllib3\connectionpool.py", line 384, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "D:\Program Files\JetBrains\project\weather\venv\lib\site-packages\urllib3\connectionpool.py", line 380, in _make_request
    httplib_response = conn.getresponse()
  File "C:\ProgramData\Anaconda3\lib\http\client.py", line 1321, in getresponse
    response.begin()
  File "C:\ProgramData\Anaconda3\lib\http\client.py", line 296, in begin
    version, status, reason = self._read_status()
  File "C:\ProgramData\Anaconda3\lib\http\client.py", line 257, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "C:\ProgramData\Anaconda3\lib\socket.py", line 589, in readinto
    return self._sock.recv_into(b)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Program Files\JetBrains\project\weather\venv\lib\site-packages\requests\adapters.py", line 449, in send
    timeout=timeout
  File "D:\Program Files\JetBrains\project\weather\venv\lib\site-packages\urllib3\connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "D:\Program Files\JetBrains\project\weather\venv\lib\site-packages\urllib3\util\retry.py", line 367, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "D:\Program Files\JetBrains\project\weather\venv\lib\site-packages\urllib3\packages\six.py", line 686, in reraise
    raise value
  File "D:\Program Files\JetBrains\project\weather\venv\lib\site-packages\urllib3\connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "D:\Program Files\JetBrains\project\weather\venv\lib\site-packages\urllib3\connectionpool.py", line 386, in _make_request
    self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
  File "D:\Program Files\JetBrains\project\weather\venv\lib\site-packages\urllib3\connectionpool.py", line 306, in _raise_timeout
    raise ReadTimeoutError(self, url, "Read timed out. (read timeout=%s)" % timeout_value)
urllib3.exceptions.ReadTimeoutError: HTTPConnectionPool(host='www.pm25.in', port=80): Read timed out. (read timeout=30)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/Program Files/JetBrains/project/weather/aqi_v8.1.py", line 63, in <module>
    main()
  File "D:/Program Files/JetBrains/project/weather/aqi_v8.1.py", line 58, in main
    city_aqi = [city_name] + get_city_aqi(city_py)
  File "D:/Program Files/JetBrains/project/weather/aqi_v8.1.py", line 18, in get_city_aqi
    r = requests.get(url, timeout=30)
  File "D:\Program Files\JetBrains\project\weather\venv\lib\site-packages\requests\api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "D:\Program Files\JetBrains\project\weather\venv\lib\site-packages\requests\api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "D:\Program Files\JetBrains\project\weather\venv\lib\site-packages\requests\sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "D:\Program Files\JetBrains\project\weather\venv\lib\site-packages\requests\sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "D:\Program Files\JetBrains\project\weather\venv\lib\site-packages\requests\adapters.py", line 529, in send
    raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPConnectionPool(host='www.pm25.in', port=80): Read timed out. (read timeout=30)

ORCA - 中国科学院大学本科四年级CS专业

赞同来自:

我猜是你的网断了,或者是网站识别出爬虫然后封了你的ip。前者好解决,后者的话你可以浏览器登录试试看看有没有被封。不过还是建议不要同ip高频爬取。

要回复问题请先登录注册