TimeoutError:[WinError 10060]连接尝试失败,因为一段时间后被连接方未正确响应,

时间:2018-10-26 14:41:06

标签: python-3.x sockets web-scraping

from urllib.request import Request, urlopen
import urllib.request,urllib.parse,urllib.error
from bs4 import BeautifulSoup
from collections import Counter

URL = 'https://www.zomato.com/bangalore/koramangala-restaurants?all=1&page=%d'
NPAGES = 1

def fetch_html(page_number):
    req = Request(URL % page_number,headers={'User-Agent': 'Mozilla/5.0'})
    ufile = urllib.request.urlopen(req)
    html = str(ufile.read().decode('utf-8'))
    return html 

执行上述代码时出现错误提示。

D:\Learning\zomato>python zomato.py
Traceback (most recent call last):
  File "zomato.py", line 40, in <module>
    run()
  File "zomato.py", line 31, in run
    htmls = [fetch_html(page_number) for page_number in range(1, NPAGES+1)]
  File "zomato.py", line 31, in <listcomp>
    htmls = [fetch_html(page_number) for page_number in range(1, NPAGES+1)]
  File "zomato.py", line 11, in fetch_html
    ufile = urllib.request.urlopen(req)
  File "C:\Program Files\Python37\lib\urllib\request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Program Files\Python37\lib\urllib\request.py", line 525, in open
    response = self._open(req, data)
  File "C:\Program Files\Python37\lib\urllib\request.py", line 543, in _open
    '_open', req)
  File "C:\Program Files\Python37\lib\urllib\request.py", line 503, in _call_chain
    result = func(*args)
  File "C:\Program Files\Python37\lib\urllib\request.py", line 1360, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "C:\Program Files\Python37\lib\urllib\request.py", line 1320, in do_open
    r = h.getresponse()
  File "C:\Program Files\Python37\lib\http\client.py", line 1321, in getresponse
    response.begin()
  File "C:\Program Files\Python37\lib\http\client.py", line 296, in begin
    version, status, reason = self._read_status()
  File "C:\Program Files\Python37\lib\http\client.py", line 257, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "C:\Program Files\Python37\lib\socket.py", line 589, in readinto
    return self._sock.recv_into(b)
  File "C:\Program Files\Python37\lib\ssl.py", line 1052, in recv_into
    return self.read(nbytes, buffer)
  File "C:\Program Files\Python37\lib\ssl.py", line 911, in read
    return 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 respon

d

我尝试从管理员命令提示符运行程序,但这也无济于事。 我在Stackoverflow的此处进行搜索,但没有找到任何相关的答案。我厌倦了那些建议,但未能解决问题。任何人都可以提出需要在我的代码中更正的建议。

0 个答案:

没有答案