本文目錄:
- 同步方式爬取博客標題
- async/await異步爬取博客標題
本片為深入理解協程系列文章的補充。
你將會在從本文中了解到:async/await
如何運用的實際的爬蟲中。
案例
從CSDN上批量爬取指定文章的標題。文章列表如下:
urls = [
'https://blog.csdn.net/Jmilk/article/details/103218919',
'https://blog.csdn.net/stven_king/article/details/103256724',
'https://blog.csdn.net/csdnnews/article/details/103154693',
'https://blog.csdn.net/dg_lee/article/details/103951021',
'https://blog.csdn.net/m0_37907797/article/details/103272967',
'https://blog.csdn.net/zzq900503/article/details/49618605',
'https://blog.csdn.net/weixin_44339238/article/details/103977138',
'https://blog.csdn.net/dengjin20104042056/article/details/103930275',
'https://blog.csdn.net/Mind_programmonkey/article/details/103940511',
'https://blog.csdn.net/xufive/article/details/102993570',
'https://blog.csdn.net/weixin_41010294/article/details/104009722',
'https://blog.csdn.net/yunqiinsight/article/details/103137022',
'https://blog.csdn.net/qq_44210563/article/details/102826406',
]
同步爬蟲
import requests
import time
from lxml import etree
urls = [
'https://blog.csdn.net/Jmilk/article/details/103218919',
'https://blog.csdn.net/stven_king/article/details/103256724',
...此處略
]
def get_title(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36'
}
r = requests.get(url, headers)
html = r.content
title = etree.HTML(html).xpath('//h1[@class="title-article"]/text()')[0]
print(title)
def main():
for url in urls:
get_title(url)
if __name__ == '__main__':
start = time.time()
main()
print(f'cost time: {time.time() - start}s')
輸出結果如下:
4G LTE/EPC 協議棧
Android-Universal-Image-Loader源碼分析
8年經驗面試官詳解 Java 面試秘訣
AES中ECB模式的加密與解密(Python3.7)
【圖解算法面試】記一次面試:說說游戲中的敏感詞過濾是如何實現的?
java進階(四)------java編程規范---代碼質量檢測工具FindBugs、PMD和CheckStyle的安裝
這是一份集合一線大廠Android工程師必備技能體系+學習路線!
【程序人生】程序員接私活常用平台匯總
你不得不了解的卷積神經網絡發展史
致 Python 初學者
OOM別慌,手把手教你定位
中國數據庫OceanBase登頂之路
網頁實現一個簡單的音樂播放器(大佬別看。(⊙﹏⊙))
cost time: 6.065227508544922s
用時:6.065227508544922s。
async/await異步爬蟲
要實現一個真正的異步爬蟲,就需要引入aiohttp
模塊,aiohttp
是一個利用asyncio的庫,可以暫時看成協程版的requests
。
import asyncio
import time
import aiohttp
from lxml import etree
urls = [
'https://blog.csdn.net/Jmilk/article/details/103218919',
'https://blog.csdn.net/stven_king/article/details/103256724',
...此處略
]
async def async_get_url(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36'
}
async with aiohttp.ClientSession() as session: # 解釋1
async with session.get(url, headers=headers) as r:
html = await r.read()
title = etree.HTML(html).xpath('//h1[@class="title-article"]/text()')[0]
print(title)
def async_main():
loop = asyncio.get_event_loop()
tasks = [async_get_url(url) for url in urls]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
if __name__ == '__main__':
start = time.time()
async_main()
print(f'cost time: {time.time() - start}s')
輸出結果:
網頁實現一個簡單的音樂播放器(大佬別看。(⊙﹏⊙))
【程序人生】程序員接私活常用平台匯總
致 Python 初學者
中國數據庫OceanBase登頂之路
Android-Universal-Image-Loader源碼分析
OOM別慌,手把手教你定位
這是一份集合一線大廠Android工程師必備技能體系+學習路線!
AES中ECB模式的加密與解密(Python3.7)
4G LTE/EPC 協議棧
【圖解算法面試】記一次面試:說說游戲中的敏感詞過濾是如何實現的?
8年經驗面試官詳解 Java 面試秘訣
java進階(四)------java編程規范---代碼質量檢測工具FindBugs、PMD和CheckStyle的安裝
你不得不了解的卷積神經網絡發展史
cost time: 0.6428999900817871s
說明:
解釋1:此處為異步的上下文管理器,是aiohttp
官方文檔提供的寫法。如果對上下文管理器不是很了解的話,可以參看【吃透Python上下文管理器】。
用時:0.6428999900817871s。從兩種爬蟲的輸出結果中可以看到:
- 文章標題的順序不同。同步爬蟲會按照
urls
內部的url順序依次爬取文章標題。而異步爬蟲爬取的順序並不完全和urls
中的url順序相同。 - 爬取速度差異很大。異步爬蟲速度大概是普通同步爬蟲的8~10倍。異步爬蟲充分利用了網絡請求這段時間。從而提高了爬取效率。
關於aiohttp
的更多用法。會在后面文章講到。
推薦閱讀
關注公眾號西加加先生
一起玩轉Python。