多进程 + 多线程抓取博客园信息
为每个进程分配一定数量的页面,然后在由进程去给线程分配待抓取页面,抓取到信息之后保存到“博客园.csv”文件中。 第三方模块 aiohttp : 协程模块 beautifulsoup4:解析模块 安装 : pip install aiohttp pip install beautifulsoup4 多线程 asyncio协程模块,通过这模块来启用任务并限制线程数量。线程多了影响效率,因为线程是通过时间分片来模拟并行,进程是真并行 多进程 multiprocessing 进程模块通过引用Pool类来启用进程池 信息保存 部分代码 import page import asyncio import os from asyncrequest import AsyncHttp from save import db from multiprocessing import Pool # 抓取信息 async def fetch(url, attach=None): reqs = AsyncHttp() text = await reqs.request(url, attach) items = page.FetchList(text) for item in items: arc = page.Article(item) db.writerow({ '作者': arc.author, '头像