python爬取百度貼吧的圖片1

python版本:2.7.10
學(xué)習(xí)python爬蟲(chóng),首先寫(xiě)了一個(gè)爬取百度貼吧圖片的程序。參考了靜覓的系列博客

好了,先上代碼:

# -*- coding : utf-8 -*-
import urllib
import urllib2
import re


class imgTest:

    def __init__(self, baseUrl, seeLZ):
        self.baseUrl = baseUrl
        self.seeLZ = '?see_lz='+str(seeLZ)
        # self.tool = Tool()
    #save a single img 
    def saveImg(self,imageURL,filename):
        u = urllib.urlopen(imageURL)
        data = u.read()
        f = open(filename,'wb')
        f.write(data)
        f.close()
    #download images
    def saveImgs(self, images, name, num):
        number = num
        for imageURL in images:
            splitPath = imageURL.split('.')
            fTail = splitPath.pop()
            if len(fTail)>3:
                fTail = "jpg"
            fileName = name+"/"+str(number)+"."+fTail
            self.saveImg(imageURL,fileName)
            number += 1
    #get img urls       
    def getAllImageURLs(self,pageNum):
        page = self.getPage(pageNum)        
        patternImg = re.compile(r'<img class="BDE_Image" pic_type="0".*?src="(.+?\.jpg)" pic_ext="jpeg"')
        images = re.findall(patternImg, page)
        for item in images:
            print item
            self.printToLog("".join(item))
            # print("\n\n")
        return images
    #print to log.txt
    def printToLog(self,mystr):
        f = open('txt/log.txt', 'a')
        # f = open('txt/log.txt')
        f.write(mystr+"\n")
        f.close()

    #get the title of the bbs
    def getTitle(self):
        page = self.getPage(1)
        pattern = re.compile('<h3 class="core_title_txt.*?>(.*?)</h3>',re.S)
        result = re.search(pattern, page)
        if result:
            self.printToLog("bbs title:"+result.group(1))
            return result.group(1).strip()
        else:
            return None
    #get the total number of the tiezi
    def getPageNum(self):
        page = self.getPage(1)
        pattern = re.compile('<li class="l_reply_num".*?<span .*?</span>.*?<span.*?>(.*?)</span>',re.S)
        result = re.search(pattern, page)
        if result:
            self.printToLog("page total num:"+result.group(1))
            return result.group(1).strip()
        else:
            return None
    #get the html source code
    def getPage(self, pageNum):
        
        try:
            url = self.baseUrl+self.seeLZ +'&pn='+str(pageNum)
            request = urllib2.Request(url)
            response = urllib2.urlopen(request)
            content = response.read()
            return content
        except urllib2.URLError, e:
            if hasattr(e, "reason"):
                print "failed to connect baidutieba.",e.reason
                return None

baseURL = 'http://tieba.baidu.com/p/3925387672'
imgtest = imgTest(baseURL,1)
totalnum = int(imgtest.getPageNum())

imageCount = 0
for i in range(1, totalnum+1):
    imageURLs = imgtest.getAllImageURLs(i)
    imgtest.saveImgs(imageURLs,"pic",imageCount)
    imageCount += len(imageURLs)
    print imageCount

由于我的sublime Text有一點(diǎn)編碼問(wèn)題我還沒(méi)來(lái)及管,所以函數(shù)的注釋就用英文注釋了(好像只有我能看懂)。最關(guān)鍵的一步就是getAllImageURLs這個(gè)函數(shù)了,需要從網(wǎng)頁(yè)中抽取到圖片的url,學(xué)好正則表達(dá)式真的很重要啊。還有一點(diǎn),就是我發(fā)現(xiàn)百度貼吧的帖子里的圖片url格式不太一樣,不同的帖子要具體的分析過(guò)才行的哦,不過(guò)呢,這個(gè)正則表達(dá)式只要稍作修改就可以滿(mǎn)足要求了。

OK,我要去爬本漫畫(huà)書(shū)來(lái)看咯:)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容