#!usr/bin/env python
# coding:utf-8
from socket import *
import urllib2
import urllib
import re
def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html
def getImg(html):
rule = r'src="(.*?\.jpg)" width'
imgre = re.compile(rule)
imglist = re.findall(imgre,html)
i = 0
for imgurl in imglist:
i += 1
urllib.urlretrieve(imgurl,'%d.jpg' %i)
html = getHtml("https://tieba.baidu.com/index.html")
print "downloading....................."
getImg(html)enter code here
When I run the code,the html and imglist could be got,the follow errors are also showed,now I don't know how to do it.
Traceback (most recent call last): File "C:/Users/18817/Desktop/getjpg.py", line 27, in getImg(html) File "C:/Users/18817/Desktop/getjpg.py", line 21, in getImg urllib.urlretrieve(imgurl,'E:/getjpg/%s.jpg' %i) File "D:\Python27\lib\urllib.py", line 98, in urlretrieve return opener.retrieve(url, filename, reporthook, data) File "D:\Python27\lib\urllib.py", line 245, in retrieve fp = self.open(url, data) File "D:\Python27\lib\urllib.py", line 213, in open return getattr(self, name)(url) File "D:\Python27\lib\urllib.py", line 457, in open_https return self.http_error(url, fp, errcode, errmsg, headers) File "D:\Python27\lib\urllib.py", line 377, in http_error result = method(url, fp, errcode, errmsg, headers) File "D:\Python27\lib\urllib.py", line 642, in http_error_302 headers, data) File "D:\Python27\lib\urllib.py", line 669, in redirect_internal return self.open(newurl) File "D:\Python27\lib\urllib.py", line 213, in open return getattr(self, name)(url) File "D:\Python27\lib\urllib.py", line 350, in open_http h.endheaders(data) File "D:\Python27\lib\httplib.py", line 1038, in endheaders self._send_output(message_body) File "D:\Python27\lib\httplib.py", line 882, in _send_output self.send(msg) File "D:\Python27\lib\httplib.py", line 844, in send self.connect() File "D:\Python27\lib\httplib.py", line 821, in connect self.timeout, self.source_address) File "D:\Python27\lib\socket.py", line 575, in create_connection raise err IOError: [Errno socket error] [Errno 10060]