先上張圖片:
小姐姐.png
首先打開網(wǎng)址:https://www.pexels.com/,然后下來會發(fā)現(xiàn)下面的圖片是慢慢的加載出來的,也就是通過Ajax請求得到的。在搜索框中輸入關(guān)鍵字:beauty,打開F12,刷新债查,選中XHR,然后一直下拉下拉:
分析2.png
會發(fā)現(xiàn)左側(cè)中的URL只有一個(gè)page是在發(fā)生變化的亚再,在通過對URL中參數(shù)的分析我嘗試的將URL中的參數(shù)js和format去掉,構(gòu)造出類似于:https://www.pexels.com/search/beauty/?page=2
其中page代表的是頁數(shù)是會發(fā)生變化的岭粤,然后復(fù)制到瀏覽器中可以打開圖片,改變page的值也沒有問題特笋。
以https://www.pexels.com/search/beauty/?page=2
為例剃浇,在瀏覽器中打開,再打開F12刷新猎物,切換到Preview選項(xiàng)卡:
分析3.png
這里面就有當(dāng)前頁面一張張圖片的信息虎囚,我們可以通過請求這個(gè)頁面,將相關(guān)圖片的鏈接解析出來蔫磨,就可以拿到我們想要的圖片了淘讥。
我們打開其中一張美女圖片,點(diǎn)擊右側(cè)的下載按鈕堤如,頁面進(jìn)行跳轉(zhuǎn):
分析4.png
從瀏覽器中發(fā)現(xiàn)圖片的地址為:
https://static.pexels.com/photos/220423/pexels-photo-220423.jpeg
這個(gè)與上圖中的 data-pin-media 屬性的值很像有沒有蒲列,多打開幾張大圖重復(fù)這個(gè)過程真是的圖片的高清地址是將data-pin-media中的images替換為static即可窒朋。
下面就可以開始寫代碼了:
打算使用PyQuery庫進(jìn)行解析,練習(xí)一下這種用法:
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)
運(yùn)行這個(gè)程序蝗岖,沒有跑起來侥猩,發(fā)生報(bào)錯(cuò):
沒有attr這個(gè)屬性,還有
報(bào)錯(cuò)01.png
Google一下:
報(bào)錯(cuò)1-Google.png
發(fā)現(xiàn)PyQuery的寫法好像有問題抵赢,小白就是這樣經(jīng)常在一個(gè)基礎(chǔ)的地方踩上坑欺劳,于是:
將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)
運(yùn)行結(jié)果如下:
運(yùn)行結(jié)果.png
但是這個(gè)下載速度實(shí)在是蛋疼的很扒稹(誰讓這個(gè)圖片這么大呢)划提,開了多進(jìn)程也一樣,而且一開始程序一直卡著我一直以為自己的代碼有什么問題跑不起來了邢享,瞎捉摸了老半天也找不出原因腔剂,后面去洗澡了,洗完后發(fā)現(xiàn)下載了幾張圖片下來了:
照片.png
所以我在想要是能寫個(gè)下載進(jìn)度條就好了驼仪,可以方便查看下載的進(jìn)度掸犬,特別是對于這種大圖片的下載,等以后學(xué)習(xí)了绪爸,可以再做些修改湾碎。