繼續鼓搗爬蟲,今天貼出一個代碼,爬取點點網「美女」標簽下的圖片,原圖。
# -*- coding: utf-8 -*- #--------------------------------------- # 程序:點點美女圖片爬蟲 # 版本:0.2 # 作者:zippera # 日期:2013-07-26 # 語言:Python 2.7 # 說明:能設置下載的頁數 #--------------------------------------- import urllib2import urllibimport re pat = re.compile('<div class="feed-big-img">/n.*?imgsrc="(ht.*?)/".*?')nexturl1 = "http://www.diandian.com/tag/%E7%BE%8E%E5%A5%B3?page=" count = 1 while count < 2: print "Page " + str(count) + "/n" myurl = nexturl1 + str(count) myres = urllib2.urlopen(myurl) mypage = myres.read() ucpage = mypage.decode("utf-8") #轉碼 mat = pat.findall(ucpage) if len(mat): cnt = 1 for item in mat: print "Page" + str(count) + " No." + str(cnt) + " url: " + item + "/n" cnt += 1 fnp = re.compile('(/w{10}/./w+)$') fnr = fnp.findall(item) if fnr: fname = fnr[0] urllib.urlretrieve(item, fname) else: print "no data" count += 1
使用方法:新建一個文件夾,把代碼保存為name.py文件,運行python name.py就可以把圖片下載到文件夾。
新聞熱點
疑難解答