本文實例講述了Python實現簡單的獲取圖片爬蟲功能。分享給大家供大家參考,具體如下:
簡單Python爬蟲,獲得網頁上的照片
#coding=utf-8import urllibimport redef getHtml(url): page = urllib.urlopen(url) html = page.read() return htmldef getImg(html): reg = r'src="(.+?/.jpg)" pic_ext' imgre = re.compile(reg) imglist = re.findall(imgre, html) return imglist// 網站地址url = "http://tieba.baidu.com/p/3368048910?pn=2"html = getHtml(url)listimg = getImg(html)x = 0for imgAddress in listimg: print imgAddress urllib.urlretrieve(imgAddress, 'image%s.jpg' % x) x+=1
希望本文所述對大家Python程序設計有所幫助。
新聞熱點
疑難解答