Python協(xié)程異步爬取數(shù)據(jù)(asyncio+aiohttp)實(shí)例
使用asyncio+aiohttp異步爬取一部小說
思路
里面涉及到異步文件的讀寫aiofiles,同時(shí)在發(fā)送請(qǐng)求時(shí)涉及到將字典轉(zhuǎn)化為字符串,接受響應(yīng)時(shí)將字符串轉(zhuǎn)化為字典,故這個(gè)里面涉及到json庫(kù),同時(shí)在請(qǐng)求下載鏈接的cid和title時(shí)使用的是同步獲取一條請(qǐng)求的響應(yīng),故其為同步操作,使用requests庫(kù)
代碼
import requests
import aiohttp
import asyncio
import json
import aiofiles
# url = 'http://dushu.baidu.com/api/pc/getCatalog?data={%22book_id%22:%224306063500%22}'
# bookid = 'http://dushu.baidu.com/api/pc/getChapterContent?data={%22book_id%22:%224306063500%22,%22cid%22:%224306063500|1569782244%22,%22need_bookinfo%22:1'
async def downloadNovel(cid,title,bid):
data2 = {
"book_id": bid,
"cid": f"{bid}|{cid}",
"need_bookinfo": 1
}
# 將字典轉(zhuǎn)化為字符串
data2 = json.dumps(data2)
# 創(chuàng)建請(qǐng)求鏈接
bookurl = f'http://dushu.baidu.com/api/pc/getChapterContent?data={data2}'
# 這里老是忘記打括號(hào) aiohttp.ClientSession()
async with aiohttp.ClientSession() as session:
async with session.get(bookurl) as resp:
# 等待結(jié)果返回
dic = await resp.json()
# 設(shè)置編碼格式encoding='utf-8'
async with aiofiles.open(f'./articles/{title}',mode = 'w',encoding='utf-8') as f:
# 異步將內(nèi)容寫入文件
await f.write(dic['data']['novel']['content'])
async def getCataList(url):
# 同步爬取所有的章節(jié)相關(guān)信息
resp = requests.get(url)
# 將返回的字符轉(zhuǎn)化為字典形式
dic = resp.json()
# print(dic)
# 創(chuàng)建一個(gè)空對(duì)象用于存儲(chǔ)異步任務(wù)
tasks = []
# 循環(huán)創(chuàng)建異步任務(wù)并且添加至tasks中
for item in dic['data']['novel']['items']:
title = item['title']
cid = item['cid']
tasks.append(asyncio.create_task(downloadNovel(cid,title,bid)))
print(title,cid)
# 執(zhí)行異步任務(wù)
await asyncio.wait(tasks)
if __name__ == '__main__':
bid = "4306063500"
url = 'http://dushu.baidu.com/api/pc/getCatalog?data={"book_id":"' + bid + '"}'
print(url)
asyncio.run(getCataList(url))效果如下

數(shù)據(jù)爬取成功
以上就是Python協(xié)程異步爬取數(shù)據(jù)(asyncio+aiohttp)實(shí)例的詳細(xì)內(nèi)容,更多關(guān)于Python協(xié)程異步爬取數(shù)據(jù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Python使用asyncio實(shí)現(xiàn)異步操作的示例
- Python中asyncio的多種用法舉例(異步同步)
- Python使用asyncio處理異步編程的代碼示例
- Python使用asyncio包實(shí)現(xiàn)異步編程方式
- Python異步庫(kù)asyncio、aiohttp詳解
- python協(xié)程異步IO中asyncio的使用
- Python使用asyncio標(biāo)準(zhǔn)庫(kù)對(duì)異步IO的支持
- Python使用asyncio異步時(shí)的常見問題總結(jié)
- Python asyncio異步編程常見問題小結(jié)
- Python asyncio異步編程簡(jiǎn)單實(shí)現(xiàn)示例
- Python中asyncio庫(kù)實(shí)現(xiàn)異步編程的示例
相關(guān)文章
Python使用JSON庫(kù)解析JSON數(shù)據(jù)的方法
這篇文章主要介紹了Python使用JSON庫(kù)解析JSON數(shù)據(jù),主要包括如何在網(wǎng)頁(yè)中獲取json數(shù)據(jù)及python內(nèi)置的json庫(kù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
Python中MySQL監(jiān)控與日志配置實(shí)戰(zhàn)指南
這篇文章主要為大家詳細(xì)介紹了Python中MySQL監(jiān)控與日志配置的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-12-12
Python udp網(wǎng)絡(luò)程序?qū)崿F(xiàn)發(fā)送、接收數(shù)據(jù)功能示例
這篇文章主要介紹了Python udp網(wǎng)絡(luò)程序?qū)崿F(xiàn)發(fā)送、接收數(shù)據(jù)功能,結(jié)合實(shí)例形式分析了Python使用socket模塊進(jìn)行udp套接字創(chuàng)建、數(shù)據(jù)收發(fā)等相關(guān)操作技巧,需要的朋友可以參考下2019-12-12
淺談numpy 函數(shù)里面的axis參數(shù)的含義
這篇文章主要介紹了numpy 函數(shù)里面的axis參數(shù)的含義,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2021-05-05
python Django連接MySQL數(shù)據(jù)庫(kù)做增刪改查
本文寫的是python Django連接MySQL數(shù)據(jù)庫(kù)的步驟,提供增刪改查的代碼2013-11-11
為了順利買到演唱會(huì)的票用Python制作了自動(dòng)搶票的腳本
大麥網(wǎng),是中國(guó)綜合類現(xiàn)場(chǎng)娛樂票務(wù)營(yíng)銷平臺(tái),業(yè)務(wù)覆蓋演唱會(huì)、 話劇、音樂劇、體育賽事等領(lǐng)域。但是因?yàn)槠睌?shù)有限,還有黃牛們不能丟了飯碗,所以導(dǎo)致了,很多人都搶不到票,那么,今天帶大家用Python來(lái)制作一個(gè)自動(dòng)搶票的腳本小程序2021-10-10

