栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python爬虫进阶:异步请求几秒钟爬光网站的全部美女图片

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python爬虫进阶:异步请求几秒钟爬光网站的全部美女图片

aiohttp模块

参考aiohttp库简单教程 - 简书

什么是aiohttp

aiohttp是一个为Python提供异步HTTP 客户端/服务端编程,基于asyncio的异步库。asyncio可以实现单线程并发IO操作,其实现了TCP、UDP、SSL等协议,aiohttp就是基于asyncio实现的http框架。

安装 
pip3 install aiohttp
 使用

在网络请求中,一个请求就是一个会话,然后aiohttp使用的是ClientSession来管理会话, 客户端会话(ClientSession)支持使用上下文管理器在结束时自动关闭。

import aiohttp
import asyncio


async def main():
    async with aiohttp.ClientSession() as session:
        async with session.get("https://www.3gbizhi.com/meinv/xgmn_2.html") as resp:
            print(await resp.text())


asyncio.run(main())

读取响应内容

# 读取文本内容
await resp.text()
# 读取非文本内容
await resp.read()
爬美女图片

目标网站:36壁纸

代码讲解

通过创建三个ClientSession来分别请求不同的内容

首先获取每个页面里每个图片的url

async def main(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            tree = etree.HTML(await response.text())
            image_url_list = tree.xpath("/html/body/div[4]/ul/li")
            for image_url in image_url_list:
                image_url = image_url.xpath("./a/@href")[0]
                await get_iamge_url(image_url)

 然后获取图片

async def get_iamge_url(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            tree = etree.HTML(await response.text())
            image = tree.xpath("//*[@id='showpicnow']/@src")[0]
            name = tree.xpath("//*[@id='showpicnow']/@alt")[0]
            path = desktop + "//" + name + ".jpg"
            await download(image, path, name)

最后写入文件

async def download(url, path, name):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            async with aiofiles.open(path, mode="wb") as f:
                await f.write(await response.read())
                print(name + "   下载完成")

创建任务列表,每个任务分别获取不同的页面

async def multiple_main():
    tasks = []
    for i in range(1, 15):
        tasks.append(main(f"https://www.3gbizhi.com/meinv/xgmn_{i}.html"))
    await asyncio.wait(tasks)


if __name__ == '__main__':
    asyncio.run(multiple_main())
效果展示

完整代码
import asyncio
import winreg
import aiofiles
import aiohttp
from lxml import etree

# 获取桌面路径
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'SoftwareMicrosoftWindowsCurrentVersionExplorerShell Folders')
desktop = winreg.QueryValueEx(key, "Desktop")[0]


async def download(url, path, name):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            async with aiofiles.open(path, mode="wb") as f:
                await f.write(await response.read())
                print(name + "   下载完成")


async def get_iamge_url(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            tree = etree.HTML(await response.text())
            image = tree.xpath("//*[@id='showpicnow']/@src")[0]
            name = tree.xpath("//*[@id='showpicnow']/@alt")[0]
            path = desktop + "//" + name + ".jpg"
            await download(image, path, name)


async def main(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            tree = etree.HTML(await response.text())
            image_url_list = tree.xpath("/html/body/div[4]/ul/li")
            for image_url in image_url_list:
                image_url = image_url.xpath("./a/@href")[0]
                await get_iamge_url(image_url)


async def multiple_main():
    tasks = []
    for i in range(1, 15):
        tasks.append(main(f"https://www.3gbizhi.com/meinv/xgmn_{i}.html"))
    await asyncio.wait(tasks)


if __name__ == '__main__':
    asyncio.run(multiple_main())

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/283531.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号