|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这个例子就是小甲鱼第54课上讲的例子,我敲了同样的代码但是报错了,请大神帮忙看看为啥报错
这是运行的代码:import urllib.request
re=urllib.request.Request('http://plackitten.com/g/500/600')
response=urllib.request.urlopen(re)
cat_img=response.read()
with open('cat_500_600.jpg','wb')as f:
f.write(cat_img)
这是报错的提示:Traceback (most recent call last):
File "C:\Program Files\Python\lib\urllib\request.py", line 1319, in do_open
h.request(req.get_method(), req.selector, req.data, headers,
File "C:\Program Files\Python\lib\http\client.py", line 1230, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Program Files\Python\lib\http\client.py", line 1276, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Program Files\Python\lib\http\client.py", line 1225, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Program Files\Python\lib\http\client.py", line 1004, in _send_output
self.send(msg)
File "C:\Program Files\Python\lib\http\client.py", line 944, in send
self.connect()
File "C:\Program Files\Python\lib\http\client.py", line 915, in connect
self.sock = self._create_connection(
File "C:\Program Files\Python\lib\socket.py", line 787, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Program Files\Python\lib\socket.py", line 918, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\houlin\Desktop\1.py", line 4, in <module>
response=urllib.request.urlopen(re)
File "C:\Program Files\Python\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\Program Files\Python\lib\urllib\request.py", line 525, in open
response = self._open(req, data)
File "C:\Program Files\Python\lib\urllib\request.py", line 542, in _open
result = self._call_chain(self.handle_open, protocol, protocol +
File "C:\Program Files\Python\lib\urllib\request.py", line 502, in _call_chain
result = func(*args)
File "C:\Program Files\Python\lib\urllib\request.py", line 1348, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "C:\Program Files\Python\lib\urllib\request.py", line 1322, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 11001] getaddrinfo failed>
import urllib.request
import easygui
while 1:
try:
z = easygui.multenterbox('请输入猫的尺寸','下载一只喵',['长','宽'])
x = z[0]
y = z[1]
response = urllib.request.urlopen('http://placekitten.com/%s/%s' % (x,y)) #爬取猫的图片
cat_img = response.read() #读取猫的图片
path = easygui.diropenbox('请选择存喵的文件夹')
with open(path + '\\cat_%s_%s.jpg' % (x,y),'wb') as f: #打开一个新的文件
f.write(cat_img) #写入猫的图片
choose = easygui.buttonbox('下载成功!','下载一只喵',['继续下载','退出'])
if choose == '退出':
break
except :
easygui.msgbox('抱歉!系统正忙!')
|
|