1. 程式人生 > >爬蟲高玩教你用Python每秒鐘下載一張高清大圖,快不快?

爬蟲高玩教你用Python每秒鐘下載一張高清大圖,快不快?

on() print async tpc 多說 xxx ima 所有 mkdir

如果爬蟲需要展現速度,我覺得就是去下載圖片吧,原本是想選擇去煎蛋那裏下載圖片的,那裏的美女圖片都是高質量的,我稿子都是差不多寫好了的,無奈今天重新看下,妹子圖的入口給關了。

至於為什麽關呢,大家可以去看看XXX日報的關停原因吧或者百度下,這裏就不多說了,這次我選擇了去下載無版權高清圖片,因為做自媒體的人很怕侵權,找無版權的圖片仿佛成了日常工作,所以這次我選擇了這個網站

https://unsplash.com/

技術分享圖片

那下面來看看使用異步以及不使用異步的差別?

技術分享圖片

(右邊是使用異步的,左邊是沒有使用異步的,由於用於測試,所以選擇下載12張圖片即可)

可以看到,在使用異步之後運行的時間比不使用異步的程序少了差不多6倍的時間,是不是感覺到high了起來?那我們分析下怎樣爬取吧。

1. 找目標網頁

這個網站首頁就有一堆圖片,而且往下拉時還會自動刷新,很明顯是個ajax加載,但不怕,動態加載這東西我們之前講過了,所以打開開發者工具看下是怎樣的請求吧。

技術分享圖片

技術分享圖片

往下拉的時候很容易看到這個請求,這個是一個get請求,狀態碼為200,網址為https://unsplash.com/napi/photos?page=3&per_page=12&order_by=latest,有三個參數,很容易知道page參數就是頁,這個參數是變化的,其他的參數都是不變的。

技術分享圖片

返回來的內容是個json類型,裏面的links下的download就是我們圖片下載的鏈接,現在所有東西都清楚了,那下面就是代碼了。

2. 代碼部分

async def __get_content(self, link):

async with aiohttp.ClientSession() as session:

response = await session.get(link)

content = await response.read()

return content

這個是獲取圖片的內容的方法,aiohttpClientSession和requests.session的用法是差不多,只不過獲取unicode編碼的方法變成了read()。

下面是完整代碼

import requests, os, time

import aiohttp, asyncio

class Spider(object):

def __init__(self):

self.headers = {

‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36‘}

self.num = 1

if ‘圖片‘ not in os.listdir(‘.‘):

os.mkdir(‘圖片‘)

self.path = os.path.join(os.path.abspath(‘.‘), ‘圖片‘)

os.chdir(self.path) # 進入文件下載路徑

async def __get_content(self, link):

async with aiohttp.ClientSession() as session:

response = await session.get(link)

content = await response.read()

return content

def __get_img_links(self, page):

url = ‘https://unsplash.com/napi/photos‘

data = {

‘page‘: page,

‘per_page‘: 12,

‘order_by‘: ‘latest‘

}

response = requests.get(url, params=data)

if response.status_code == 200:

return response.json()

else:

print(‘請求失敗,狀態碼為%s‘ % response.status_code)

async def __download_img(self, img):

content = await self.__get_content(img[1])

with open(img[0]+‘.jpg‘, ‘wb‘) as f:

f.write(content)

print(‘下載第%s張圖片成功‘ % self.num)

self.num += 1

def run(self):

start = time.time()

for x in range(1, 101): # 下載一百頁的圖片就可以了,或者自己更改頁數

links = self.__get_img_links(x)

tasks = [asyncio.ensure_future(self.__download_img((link[‘id‘], link[‘links‘][‘download‘]))) for link in links]

loop = asyncio.get_event_loop()

loop.run_until_complete(asyncio.wait(tasks))

if self.num >= 10: # 測試速度使用,如需要下載多張圖片可以註釋這段代碼

break

end = time.time()

print(‘共運行了%s秒‘ % (end-start))

def main():

spider = Spider()

spider.run()

if __name__ == ‘__main__‘:

main()

可以看到不到50行的代碼就可以把整個網網站的圖片下載下來了,不得不吹一下python的強大~~~

技術分享圖片

福利時間:

技術分享圖片

進群:125240963 即可獲取數十套PDF哦!

爬蟲高玩教你用Python每秒鐘下載一張高清大圖,快不快?