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感知線程狀態(tài)的解決方案之Event與信號(hào)量
本文主要介紹了如何感知線程狀態(tài)、如何停止一個(gè)線程、線程之間的Event用法2021-06-06淺談Python使用pickle模塊序列化數(shù)據(jù)優(yōu)化代碼的方法
這篇文章主要介紹了淺談Python使用pickle模塊序列化數(shù)據(jù)優(yōu)化代碼的方法,pickle模塊可以對(duì)多種Python對(duì)象進(jìn)行序列化和反序列化,序列化稱為pickling,反序列化稱為unpickling,需要的朋友可以參考下2023-07-07Python的SQLalchemy模塊連接與操作MySQL的基礎(chǔ)示例
SQLalchemy是Python世界中驅(qū)動(dòng)MySQL的一款高人氣模塊,這里我們從入門開始來看一下Python的SQLalchemy模塊連接與操作MySQL的基礎(chǔ)示例:2016-07-07python實(shí)現(xiàn)數(shù)據(jù)圖表
plotly是現(xiàn)代平臺(tái)的敏捷商業(yè)智能和數(shù)據(jù)科學(xué)庫(kù),它作為一款開源的繪圖庫(kù),可以應(yīng)用于Python、R、MATLAB、Excel、JavaScript和jupyter等多種語(yǔ)言,主要使用的js進(jìn)行圖形繪制,實(shí)現(xiàn)過程中主要就是調(diào)用plotly的函數(shù)接口,底層實(shí)現(xiàn)完全被隱藏,便于初學(xué)者的掌握。2017-07-07