python爬淘宝网图片老出<urlopen error [Errno 11004] getaddrinfo failed>

0
用以下python程序尝试循环打开淘宝网搜索页下的100个分页,但老是打开30个左右就报错了,老是出11004:getaddrinfo failed错误,代码如下:
import urllib.request import re import urllib.error import time keyname='羽绒服' keyname_code=urllib.request.quote(keyname) url2="https://s.taobao.com/list%3Fsp ... ot%3B headers=("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0") opener=urllib.request.build_opener() ##build_opener是什么? ->创建开瓶器(打开网页工具) opener.addheaders=[headers] ##addheaders怎么用? ->往开瓶器中增加报头 urllib.request.install_opener(opener) for i in range(0,100): try: url=url2+str(i*60) data=urllib.request.urlopen(url) url_status = data.getcode() print("第"+str(i+1)+"次:"+str(url_status)) time.sleep(10) ##延迟3秒 except urllib.error.HTTPError as e: print(e.code) print(e.reason)
 
报错情况:Traceback (most recent call last):
  File "D:\Program Files\Python35-32\lib\urllib\request.py", line 1254, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "D:\Program Files\Python35-32\lib\http\client.py", line 1106, in request
    self._send_request(method, url, body, headers)
  File "D:\Program Files\Python35-32\lib\http\client.py", line 1151, in _send_request
    self.endheaders(body)
  File "D:\Program Files\Python35-32\lib\http\client.py", line 1102, in endheaders
    self._send_output(message_body)
  File "D:\Program Files\Python35-32\lib\http\client.py", line 934, in _send_output
    self.send(msg)
  File "D:\Program Files\Python35-32\lib\http\client.py", line 877, in send
    self.connect()
  File "D:\Program Files\Python35-32\lib\http\client.py", line 1252, in connect
    super().connect()
  File "D:\Program Files\Python35-32\lib\http\client.py", line 849, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "D:\Program Files\Python35-32\lib\socket.py", line 693, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "D:\Program Files\Python35-32\lib\socket.py", line 732, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11004] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "F:/python/project/mmds/pytest01.py", line 19, in <module>
    data=urllib.request.urlopen(url)
  File "D:\Program Files\Python35-32\lib\urllib\request.py", line 163, in urlopen
    return opener.open(url, data, timeout)
  File "D:\Program Files\Python35-32\lib\urllib\request.py", line 466, in open
    response = self._open(req, data)
  File "D:\Program Files\Python35-32\lib\urllib\request.py", line 484, in _open
    '_open', req)
  File "D:\Program Files\Python35-32\lib\urllib\request.py", line 444, in _call_chain
    result = func(*args)
  File "D:\Program Files\Python35-32\lib\urllib\request.py", line 1297, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "D:\Program Files\Python35-32\lib\urllib\request.py", line 1256, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 11004] getaddrinfo failed>

Process finished with exit code 1
已邀请:

要回复问题请先登录注册