python3爬取1024圖片

這兩年python特別火,火到博客園如今也是隔三差五的出現一些python的文章。各類開源軟件、各類爬蟲算法紛紛開路,做爲互聯網行業的IT狗天然看的我也是心癢癢,因而趁着這個霧霾橫行的週末瞅了兩眼,做爲一名老司機以爲仍是應該以練帶學,1024在程序員界這麼流行的網站,固然拿來先練一練。php

python自稱是以天然語言的視角來編程,特色是開發快,語言簡潔,沒那麼多技巧,大名鼎鼎的豆瓣、youtube都是使用python開發的網站,看來python在大規模使用這個方面來說應該沒有啥子問題;python也不是沒有缺點在性能方面就Java、C++等老前輩仍是沒得比的,另外python和nodejs同樣只能使用CPU單核,也是性能方面影響是因素之一。但python在特定領域表現突出,特別是腳本、爬蟲、科學算法等。html

好了,仍是說正事如何爬取1024網站的圖片node


分析


列表頁面

首先進入1024的導航網站,隨便點擊一個地址進入選擇圖片區或者在網站地址後面添加thread0806.php?fid=16&search=&page=,這就是1024網站的圖片區,這個爬蟲就是主要抓取這個區域的全部圖片,使用瀏覽器debug分析一下這個頁面發現基本都是列表頁,格式以下:python

list

在地址欄http://xxxxxx.biz/thread0806.php?fid=16&search=&page=後面拼一、二、3等於就是訪問圖片區第一頁、第二頁、第三頁的列表頁。根據這些列表頁就能夠爬出具體的每個圖片頁的地址,相似上圖的地址:htm_data/16/1611/2114702.html 在地址的前面拼接上主站地址就是具體的圖片頁了。因此根據以上的分析:經過循環地址欄找到不一樣的列表頁在根據列表頁找到具體的圖片頁git

地址欄->圖片列表->圖片頁地址程序員

獲取列表頁圖片地址代碼以下:github

import urllib.request,socket,re,sys,os

baseUrl='http://xxxx.biz/'

def getContant(Weburl):
    Webheader= {'Upgrade-Insecure-Requests':'1',
                'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36',}
    req = urllib.request.Request(url = Weburl,headers=Webheader)
    respose = urllib.request.urlopen(req)
    _contant = respose.read()
    respose.close()
    return str(_contant)

def getUrl(URL):
    pageIndex = 1
    for i in range(1,int(pageIndex)+1):
        Weburl = URL + str(i)
        contant = getContant(Weburl)
        comp = re.compile(r'<a href="htm_data.{0,30}html" target="_blank" id=""><font color=g')
        urlList1 = comp.findall(contant)
        comp = re.compile(r'a href="(.*?)"')
        urlList2 = comp.findall(str(urlList1))
        urlList = []
        for url1 in urlList2:
            url2 = baseUrl+url1
            urlList.append(url2)
        return urlList
        
URL = baseUrl+'thread0806.php?fid=16&search=&page='
UrlList = getUrl(URL) 
print(UrlList)

在這個地址後面拼接1到N就是不一樣的列表頁正則表達式


圖片頁面

利用瀏覽器debug一下頁面,圖片基本上都是外鏈地址,以http或者https開頭以jpg、png、gif結尾,寫個正則表達式匹配這些地址,而後交給程序下載就OK了。算法

頁面代碼以下:編程

page

在下載過程當中遇到了幾個問題,就是有的頁面會報403禁止訪問等,應該是網站加了一些防止爬蟲的手段,網上找了下加上header參數來模擬瀏覽器訪問就解決了;

下載單個頁面代碼以下:

import urllib.request,socket,re,sys,os

#定義文件保存路徑
targetPath = "D:\\temp\\1024\\1"

def openUrl(url):
    headers = {
                  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '                            'Chrome/51.0.2704.63 Safari/537.36'
               }

    req = urllib.request.Request(url=url, headers=headers)
    res = urllib.request.urlopen(req)
    data = res.read()
    downImg(data)

def downImg(data):
    for link,t in set(re.findall(r'([http|https]:[^\s]*?(jpg|png|gif))', str(data))):

        if link.startswith('s'):
            link='http'+link
        else:
            link='htt'+link
        print(link)
        try:
            opener=urllib.request.build_opener()
            opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36')]
            urllib.request.install_opener(opener)
            urllib.request.urlretrieve(link,saveFile(link))
        except:
            print('失敗')

def saveFile(path):
    #檢測當前路徑的有效性
    if not os.path.isdir(targetPath):
        os.mkdir(targetPath)

    #設置每一個圖片的路徑
    pos = path.rindex('/')
    t = os.path.join(targetPath,path[pos+1:])
    return t

url = "http://xxxx.biz/htm_data/16/1611/2115193.html"
openUrl(url)


批量爬取

批量爬取有兩個工做要作,第一for循環目標內的全部列表頁,第二爲了不重複爬取,須要給每一個頁面創建惟一的文件夾,下次爬取的時候若是存在直接跳過。最後在理一下全部的爬取步驟:

循環地址欄->找出圖片頁列表->圖片頁分析找出圖片地址->爲圖片頁創建惟一的文件夾->開始下載頁面圖片

完整的代碼以下:

import urllib.request,socket,re,sys,os

baseUrl='http://xxxx.biz/'
targetPath = "D:\\temp\\1024\\"

def getContant(Weburl):
    Webheader= {'Upgrade-Insecure-Requests':'1',
                'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36',}
    req = urllib.request.Request(url = Weburl,headers=Webheader)
    respose = urllib.request.urlopen(req)
    _contant = respose.read()
    respose.close()
    return str(_contant)

def getUrl(URL):
    pageIndex = 1
    for i in range(1,int(pageIndex)+1):
        Weburl = URL + str(i)
        contant = getContant(Weburl)
        comp = re.compile(r'<a href="htm_data.{0,30}html" target="_blank" id=""><font color=g')
        urlList1 = comp.findall(contant)
        comp = re.compile(r'a href="(.*?)"')
        urlList2 = comp.findall(str(urlList1))
        urlList = []
        for url1 in urlList2:
            url2 = baseUrl+url1
            urlList.append(url2)
        return urlList

def openUrl(url):
    headers = {
                  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '                            'Chrome/51.0.2704.63 Safari/537.36'
               }

    filePath=targetPath+url[-12:-5]
    #檢測當前路徑的有效性
    if not os.path.isdir(filePath):
        os.mkdir(filePath)
        req = urllib.request.Request(url=url, headers=headers)
        res = urllib.request.urlopen(req)
        data = res.read()
        downImg(data,filePath)
    else:
        print("已經下載過的地址跳過:"+url)
        print("filePath  "+filePath)

def downImg(data,filePath):
    for link,t in set(re.findall(r'([http|https]:[^\s]*?(jpg|png|gif))', str(data))):

        if link.startswith('s'):
            link='http'+link
        else:
            link='htt'+link
        print(link)
        try:
            opener=urllib.request.build_opener()
            opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36')]
            urllib.request.install_opener(opener)
            urllib.request.urlretrieve(link,saveFile(link,filePath))
        except:
            print('失敗')

def saveFile(path,filePath):
    #設置每一個圖片的路徑
    pos = path.rindex('/')
    t = os.path.join(filePath,path[pos+1:])
    return t


def openPage(UrlList):
    for pageUlr in UrlList:
        try:
            print('正在下載地址:'+pageUlr)
            openUrl(pageUlr)
        except:
            print('地址:'+pageUlr+'下載失敗')

URL = baseUrl+'thread0806.php?fid=16&search=&page='
for num in range(0,20):#0-20頁
    print("#######################################")
    print("##########總目錄下載地址###############")
    print(URL+str(num))
    print("#######################################")
    print("#######################################")
    UrlList = getUrl(URL+str(num)) 
    openPage(UrlList)

最後的爬取結果:

result

源代碼地址:python-crawler
具體地址和源代碼在一塊兒


其它

關於python2和python3的爭論,網站爭論比較大python3不兼容pyhton2,不少第三方的類庫暫時尚未支持python3等等,可是對於咱們新手來講,確定是往前看果斷python3.

代碼比較冗餘幾個地方尚未寫好,還在慢慢學習中,目前只是搞的能夠跑起來。還有幾個問題沒有解決,下載一段時間後會莫名其妙的斷掉目前還麼找到緣由,後期看是否能夠加上多線程來爬取可能會快一點,你們有什麼更好的建議也能夠提出來。


參考:

爬取豆瓣首頁圖片

使用Python爬取1024上的圖片


做者:純潔的微笑
出處:http://www.ityouknow.com/
版權全部,歡迎保留原文連接進行轉載:)

相關文章
相關標籤/搜索