针对问题整理的纯粹代码:
import urllib.request
urllib.request.urlretrieve('http://imgsize.ph.126.net/?enlarge=true&imgurl=http://img0.ph.126.net/x7rKXzt8JbikVn6btnkzDg==/6630911635050687771.png_450x250x0x95.png', 'F:\\workspace-python\\hw_02\\img_dl\\a.png')
print("a done!")
urllib.request.urlretrieve('http://data.whicdn.com/images/203937856/superthumb.png', 'F:\\workspace-python\\hw_02\\img_dl\\b.png')
print("b done!")完整的报错信息:
"D:\Program Files\Python35\python.exe" F:/workspace-python/hw_02/hw_04_01.py
a done!
Traceback (most recent call last):
File "D:\Program Files\Python35\lib\urllib\request.py", line 1254, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "D:\Program Files\Python35\lib\http\client.py", line 1106, in request
self._send_request(method, url, body, headers)
File "D:\Program Files\Python35\lib\http\client.py", line 1151, in _send_request
self.endheaders(body)
File "D:\Program Files\Python35\lib\http\client.py", line 1102, in endheaders
self._send_output(message_body)
File "D:\Program Files\Python35\lib\http\client.py", line 934, in _send_output
self.send(msg)
File "D:\Program Files\Python35\lib\http\client.py", line 877, in send
self.connect()
File "D:\Program Files\Python35\lib\http\client.py", line 849, in connect
(self.host,self.port), self.timeout, self.source_address)
File "D:\Program Files\Python35\lib\socket.py", line 711, in create_connection
raise err
File "D:\Program Files\Python35\lib\socket.py", line 702, in create_connection
sock.connect(sa)
OSError: [WinError 10013] 以一种访问权限不允许的方式做了一个访问套接字的尝试。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "F:/workspace-python/hw_02/hw_04_01.py", line 5, in <module>
urllib.request.urlretrieve('http://data.whicdn.com/images/203937856/superthumb.png', 'F:\\workspace-python\\hw_02\\img_dl\\b.png')
File "D:\Program Files\Python35\lib\urllib\request.py", line 188, in urlretrieve
with contextlib.closing(urlopen(url, data)) as fp:
File "D:\Program Files\Python35\lib\urllib\request.py", line 163, in urlopen
return opener.open(url, data, timeout)
File "D:\Program Files\Python35\lib\urllib\request.py", line 466, in open
response = self._open(req, data)
File "D:\Program Files\Python35\lib\urllib\request.py", line 484, in _open
'_open', req)
File "D:\Program Files\Python35\lib\urllib\request.py", line 444, in _call_chain
result = func(*args)
File "D:\Program Files\Python35\lib\urllib\request.py", line 1282, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "D:\Program Files\Python35\lib\urllib\request.py", line 1256, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [WinError 10013] 以一种访问权限不允许的方式做了一个访问套接字的尝试。>
Process finished with exit code 1
可以看到第一个网易的图片是可以下载成功的,但习题中要求的网站却无论如何无法下载成功。
图片URL获取到的是正确的,可以直接在浏览器中访问。我开了VPN,应该不会是代理的问题吧?
网上搜了下,基本上都提到是端口已占用,但是我们的场景是作为客户端,应该不涉及监听端口的问题才对。
而且网易的图片可以下载,感觉应该是目标网站有什么特别之处才对~