|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
看小甲鱼老师视频教学中有这么一个程序,小甲鱼老师run之后是可以执行的,但是我的程序执行之后就报错了,后面是报错信息,请大神看看怎么回事,如何解决呢?谢谢!
import urllib.request
import re
import os
def open_url(url):
req = urllib.request.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36')
page = urllib.request.urlopen(req)
html = page.read().decode('utf - 8')
return html
def get_img(html):
p = r'<img class = "BDE_Image" src = "([^"]+\.jpg)"'
imglist = re.findall(p, html)
for each in imglist:
print(each)
'''
try:
os.mkdir('NewPics')
for each in imglist:
filename = each.split('/')[-1]
urllib.request.urlretrieve(each, filename, None)
'''
if __name__ == '__main__':
url = 'http://tieba.baidu.comm/p/3823765471'
get_img(open_url(url))
run之后报错信息如下:
=========== RESTART: E:/Python_study/p14_10.py ===========
Traceback (most recent call last):
File "E:\Web_develop\python\lib\urllib\request.py", line 1318, in do_open
encode_chunked=req.has_header('Transfer-encoding'))
File "E:\Web_develop\python\lib\http\client.py", line 1239, in request
self._send_request(method, url, body, headers, encode_chunked)
File "E:\Web_develop\python\lib\http\client.py", line 1285, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "E:\Web_develop\python\lib\http\client.py", line 1234, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "E:\Web_develop\python\lib\http\client.py", line 1026, in _send_output
self.send(msg)
File "E:\Web_develop\python\lib\http\client.py", line 964, in send
self.connect()
File "E:\Web_develop\python\lib\http\client.py", line 936, in connect
(self.host,self.port), self.timeout, self.source_address)
File "E:\Web_develop\python\lib\socket.py", line 704, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "E:\Web_develop\python\lib\socket.py", line 745, 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 "E:/Python_study/p14_10.py", line 31, in <module>
get_img(open_url(url))
File "E:/Python_study/p14_10.py", line 11, in open_url
page = urllib.request.urlopen(req)
File "E:\Web_develop\python\lib\urllib\request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "E:\Web_develop\python\lib\urllib\request.py", line 526, in open
response = self._open(req, data)
File "E:\Web_develop\python\lib\urllib\request.py", line 544, in _open
'_open', req)
File "E:\Web_develop\python\lib\urllib\request.py", line 504, in _call_chain
result = func(*args)
File "E:\Web_develop\python\lib\urllib\request.py", line 1346, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "E:\Web_develop\python\lib\urllib\request.py", line 1320, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 11001] getaddrinfo failed>
|
|