1. 程式人生 > >Python爬蟲入門-python之爬取pexels高清圖片

Python爬蟲入門-python之爬取pexels高清圖片

先上張圖片:
小姐姐.png

首先開啟網址:,然後下來會發現下面的圖片是慢慢的加載出來的,也就是通過Ajax請求得到的。在搜尋框中輸入關鍵字:beauty,開啟F12,重新整理,選中XHR,然後一直下拉下拉:

分析2.png

會發現左側中的URL只有一個page是在發生變化的,在通過對URL中引數的分析我嘗試的將URL中的引數js和format去掉,構造出類似於:https://www.pexels.com/search/beauty/?page=2
其中page代表的是頁數是會發生變化的,然後複製到瀏覽器中可以開啟圖片,改變page的值也沒有問題。
https://www.pexels.com/search/beauty/?page=2

為例,在瀏覽器中開啟,再開啟F12重新整理,切換到Preview選項卡:

分析3.png

這裡面就有當前頁面一張張圖片的資訊,我們可以通過請求這個頁面,將相關圖片的連結解析出來,就可以拿到我們想要的圖片了。
我們開啟其中一張美女圖片,點選右側的下載按鈕,頁面進行跳轉:

分析4.png

從瀏覽器中發現圖片的地址為:
https://static.pexels.com/photos/220423/pexels-photo-220423.jpeg
這個與上圖中的 data-pin-media 屬性的值很像有沒有,多開啟幾張大圖重複這個過程真是的圖片的高清地址是將data-pin-media中的images替換為static即可。
下面就可以開始寫程式碼了:
打算使用PyQuery

庫進行解析,練習一下這種用法:

import requests
from requests.exceptions import RequestException
from pyquery import PyQuery as pq

keyword='beauty'
headers={
 'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'accept-encoding':'gzip, deflate,sdch,br',
 'cookie':'__cfduid=d3e43ad7f4bb07152deb3e9b4ca571b271505889614; locale=en; _ga=GA1.2.127776053.1505890636; _gid=GA1.2.783458515.1505890636; _gat=1'
, 'user-agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36' } def get_index(url): response=requests.get(url,headers=headers) try: if response.status_code==200: return response.text return None except RequestException: return None def parse_index(html): doc=pq(html) links=doc('.photos .photo-item a img') for link in links: # title=link.attr('alt').replace(',','') url=link.attr('data-pin-media').replace('images','static').split('?')[0] yield url def main(page): url = 'https://www.pexels.com/search/'+keyword+'/?page='+str(page) html=get_index(url) if html: urls=parse_index(html) print(urls) if __name__=='__main__': main(1)

執行這個程式,沒有跑起來,發生報錯:
沒有attr這個屬性,還有

報錯01.png

Google一下:

報錯1-Google.png

發現PyQuery的寫法好像有問題,小白就是這樣經常在一個基礎的地方踩上坑,於是:
url=link.attr(‘data-pin-media’).replace(‘images’,’static’).split(‘?’)[0],改成:url=pq(link).attr(‘data-pin-media’).replace(‘images’,’static’).split(‘?’)[0]
可以跑起來了。
然後就是儲存圖片:

def download_img(url):
    response=requests.get(url)
    try:
        if response.status_code==200:
            return response.content
        return None
    except RequestException:
        return None

def save_image(content):
    path_name='{0}/{1}.{2}'.format(os.getcwd(),md5(content).hexdigest(),'jpg')
        if not os.path.exists(path_name):
            with open(path_name,'wb') as f:
                f.write(content)
                f.close()

def main(page):
    url = 'https://www.pexels.com/search/'+keyword+'/?page='+str(page)
    html=get_index(url)
    if html:
        urls=parse_index(html)
        for url in urls:
            print('正在下載:%r'%url)
            content=download_img(url)
            save_image(content)
            print('下載完成:%r'%url)
            time.sleep(3)

執行結果如下:

執行結果.png

但是這個下載速度實在是蛋疼的很啊(誰讓這個圖片這麼大呢),開了多程序也一樣,而且一開始程式一直卡著我一直以為自己的程式碼有什麼問題跑不起來了,瞎捉摸了老半天也找不出原因,後面去洗澡了,洗完後發現下載了幾張圖片下來了:

照片.png

所以我在想要是能寫個下載進度條就好了,可以方便檢視下載的進度,特別是對於這種大圖片的下載,等以後學習了,可以再做些修改。