python抓取网页图片并放到指定文件夹

python抓取网站图片并放到指定文件夹

代码如下:

# -*- coding=utf-8 -*-import urllib2import urllibimport socketimport osimport redef docment(): print u’把文件存在e:\python\图(请输入数字或字母)’ h=raw_input() path=u’e:\python\图’+str(h) if not os.path.exists(path): os.makedirs(path) return pathdef getallurl(html): reg=r”a href='(.*?\.htm)'” allurl= re.compile(reg) alllist = re.findall(allurl,html) return alllistdef gethtml(url): url=url req_header = {‘user-agent’:’mozilla/5.0 (windows; u; windows nt 6.1; en-us; rv:1.9.1.6) gecko/20091201 firefox/3.5.6′} req_timeout = 20 html=’cuowu’ try: req = urllib2.request(url,none,req_header) resp = urllib2.urlopen(req,none,req_timeout) html = resp.read() except urllib2.urlerror as e: print e.message except socket.timeout as e: gethtml(url,fu) return htmldef getimg(html,path): reg = r’img imgre= re.compile(reg) imglist = re.findall(imgre, html) if imglist: print ‘ghasghg’,path for imgurl in imglist: print imgurl content2=urllib2.urlopen(imgurl).read() with open(path+’/’+imgurl[-7:],’wb’) as code: code.write(content2) else: return 0def getallurl(html): reg=r”a href='(.*?\.htm)'” allurl= re.compile(reg) alllist = re.findall(allurl,html) return alllistj=1i=0print u’请输入网址:’ul=raw_input()print u’开始下载’print u’第’+str(j)+u’页’html=gethtml(ul)alllist=getallurl(html)path=docment()getimg(html,path)while i

发表评论