Python 爬蟲網頁抓圖儲存,python爬蟲網頁抓圖

來源:互聯網
上載者:User

Python 爬蟲網頁抓圖儲存,python爬蟲網頁抓圖

網站選擇案頭壁紙網站的汽車主題:

下面的兩個print在調試時開啟

#print tag#print attrs

#!/usr/bin/env pythonimport reimport urllib2import HTMLParserbase = "http://desk.zol.com.cn"path = '/home/mk/cars/'star = ''def get_url(html):parser = parse(False)request = urllib2.Request(html)response = urllib2.urlopen(request)resp = response.read()parser.feed(resp)def download(url):content = urllib2.urlopen(url).read()format = '[0-9]*\.jpg';res = re.search(format,url);print 'downloading:',res.group()filename = path+res.group()f = open(filename,'w+')f.write(content)f.close() class parse(HTMLParser.HTMLParser):def __init__(self,Index):self.Index = Index;HTMLParser.HTMLParser.__init__(self)def handle_starttag(self,tag,attrs):#print tag#print attrsif(self.Index):if not cmp(tag,'a'):if(len(attrs) == 4):if(attrs[0] ==('class','pic')):#print tag#print attrsnew = base+attrs[1][1]print 'found a link:',newglobal starstar = newget_url(new)else:if not cmp(tag,'img'):if(attrs[0] == ('id','bigImg')):#print tag#print attrsImage_url = attrs[1][1]print 'found a picture:',Image_urldownload(Image_url)if not cmp(tag,'a'):if (len(attrs) == 4):if (attrs[1] == ('class','next')):#print tag#print attrsnext = base + attrs[2][1]print 'found a link:',nextif (star != next):get_url(next)Index_url = 'http://desk.zol.com.cn/qiche/'con = urllib2.urlopen(Index_url).read()Parser_index = parse(True)Parser_index.feed(con)



僅僅就是抓案頭壁紙網站上的優美的壁紙 。。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.