代码之家  ›  专栏  ›  技术社区  ›  Nrusingh Prasad Acharya

请求。输入python时出现连接超时错误

  •  4
  • Nrusingh Prasad Acharya  · 技术社区  · 7 年前

    语言版本:Python 3.6.3
    IDE版本:PyCharm 2017.2.3

    我试图解析一个天气网站来打印一个地方的天气。在学习Python时,我以前使用 urllib。要求url打开(url)。读取() 它成功了。现在,我将代码修改为 BeautifulSoup4 请求 单元以下是我的代码:

    from bs4 import *
    import requests
    url = "https://www.accuweather.com/en/in/dhenkanal/189844/weather-forecast/189844"
    data = requests.get(url)
    soup = BeautifulSoup(data.text, "html.parser")
    print(soup.find('div', {'class': 'info'}))
    

    但每次我尝试运行代码时,都会出现以下错误:

    Traceback (most recent call last):
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 601, in urlopen
    chunked=chunked)
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 387, in _make_request
    six.raise_from(e, None)
    File "", line 2, in raise_from
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 383, in _make_request
    httplib_response = conn.getresponse()
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 1331, in getresponse
    response.begin()
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 297, in begin
    version, status, reason = self._read_status()
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\socket.py", line 586, in readinto
    return self._sock.recv_into(b)
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\ssl.py", line 1009, in recv_into
    return self.read(nbytes, buffer)
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\ssl.py", line 871, in read
    return self._sslobj.read(len, buffer)
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\ssl.py", line 631, in read
    v = self._sslobj.read(len, buffer)
    TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\adapters.py", line 440, in send
    timeout=timeout
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 639, in urlopen
    _stacktrace=sys.exc_info()[2])
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\util\retry.py", line 357, in increment
    raise six.reraise(type(error), error, _stacktrace)
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\packages\six.py", line 685, in reraise
    raise value.with_traceback(tb)
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 601, in urlopen
    chunked=chunked)
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 387, in _make_request
    six.raise_from(e, None)
    File "", line 2, in raise_from
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 383, in _make_request
    httplib_response = conn.getresponse()
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 1331, in getresponse
    response.begin()
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 297, in begin
    version, status, reason = self._read_status()
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\socket.py", line 586, in readinto
    return self._sock.recv_into(b)
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\ssl.py", line 1009, in recv_into
    return self.read(nbytes, buffer)
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\ssl.py", line 871, in read
    return self._sslobj.read(len, buffer)
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\ssl.py", line 631, in read
    v = self._sslobj.read(len, buffer)
    urllib3.exceptions.ProtocolError: ('Connection aborted.', TimeoutError(10060, 'A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond', None, 10060, None))
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
    File "E:/Projects/Python/Practice/Practice1.py", line 5, in 
    data = requests.get(url)
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\api.py", line 72, in get
    return request('get', url, params=params, **kwargs)
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
    File "C:\Users\Nrusingh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\adapters.py", line 490, in send
    raise ConnectionError(err, request=request)
    requests.exceptions.ConnectionError: ('Connection aborted.', TimeoutError(10060, 'A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond', None, 10060, None))
    
    Process finished with exit code 1  
    

    这个错误是什么?如何纠正?为什么它在urllib中有效,而在请求中无效?

    2 回复  |  直到 7 年前
        1
  •  4
  •   dina    7 年前

    我直接使用了你的代码,我得到了同样的错误,然后我遵循了在浏览器中发送请求的方式。如果预期的标头未随请求一起发送,则某些服务器不会响应,这些请求将用作后端处理的一部分。结果表明服务器正在查找名为 user-agent 通常用于确定请求来自哪个客户端。现在,修改了下面的代码!

    from bs4 import *
    import requests
    url = "https://www.accuweather.com/en/in/dhenkanal/189844/weather-forecast/189844"
    
    headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'}
    
    data = requests.get(url, headers=headers)
    soup = BeautifulSoup(data.text, "html.parser")
    

    现在你可以玩你的汤了! 实际上,您可以传递更多的标题,如 accept, dnt, pragma, accept-language, cache-control 等等。这些http头的解释是为了另一个问题,另一次。希望有帮助:)

        2
  •  1
  •   Argus Malware    7 年前

    尝试增加请求的超时参数。获取方法:

    requests.get(url, headers=headers, timeout=5)
    

    但是如果您的脚本被服务器阻止,以防止取消尝试。如果是这种情况,您可以通过设置适当的标题来尝试伪造web浏览器。

    {"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 GTB7.1 (.NET CLR 3.5.30729)", "Referer": "http://example.com"}
    

    您的最终代码

    import requests
    url = "https://www.accuweather.com/en/in/dhenkanal/189844/weather-forecast/189844"
    headers = {"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 GTB7.1 (.NET CLR 3.5.30729)", "Referer": "http://example.com"}
    data = requests.get(url,headers=headers,timeout=5)