內(nèi)涵:正則匹配,正則替換,頁面抓取,圖片保存 。
實用的第一次 Python 代碼 參考
#!/usr/bin/env pythonimport urllibimport re x=0def getHtml(url): page = urllib.urlopen(url) html = page.read() return html def getImg(html): global x reg = 'alt=".+?" src="(.+?/.jpg)"' imgre = re.compile(reg) imglist = re.findall(imgre,html) for imgurl in imglist: urllib.urlretrieve(re.sub(r',/d+,/d+',',800,450',imgurl),"img/%s.jpg" % x) print "/n"+re.sub(r',/d+,/d+',',800,450',imgurl)+"========"+"img/%s.jpg" % x x+=1 print 'Starting...' pages = range(1,9) for p in pages: html = getHtml('http://m.lovebizhi.com/category/7655/%d/' % p) print "/n-------------------------page:%d-------------------------------" % p getImg(html) print "/nDone!"
以上所述就是本文給大家分享的全部代碼了,本人Python菜鳥,第一個作品,希望對大家能有所幫助。
新聞熱點
疑難解答