简单比较一下同步|多线程|协程的爬取速度

2644 篇文章 26 订阅
2348 篇文章 14 订阅

「本章,啥也不干,就来简略的比较比较爬虫速度」

先上结果,以下结果是多次运行后取的最优结果。不同时间段对于速率影响还是有的。参考即可

"""
普通函数执行:总耗时 3.330171585083008 S
线程池执行:总耗时 总耗时 1.6058530807495117 S
多线程执行:总耗时 总耗时 1.8512330055236816 S
协程异步执行:总耗时 总耗时 总耗时 1.091230869293213 S
线程池协程异步:总耗时 0.8936080932617188 S
"""

普通函数

也就是同步爬虫

import requests
import time

List_Url = ['https://img.soutula.com/large/006APoFYly8hbhsbsmciuj30hs0hfaam.jpg',
            'https://img.soutula.com/large/ceeb653ely8hbhrnv8vx1j20hs0hj3zo.jpg',
            'https://img.soutula.com/large/006APoFYly8hbhm20mb6rj30hs0hmt9j.jpg',
            'https://img.soutula.com/large/ceeb653ely8hbhixn8cvvj205k0563yf.jpg',
            'https://img.soutula.com/large/ceeb653ely8hbhbzwzy4ej20hs0f1753.jpg']

def run():
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36 Edg/91.0.864.53',
        'Referer': 'https://www.fabiaoqing.com/biaoqing'
    }
    for Url in List_Url:
        response = requests.get(Url,headers=headers)
        with open("爬fabiaoqing网图/" + Url[-8:-4] + '.jpg', 'wb') as w:
            content =response.content
            w.write(content)

if __name__ == '__main__':
    s = time.time()
    run()
    print("总耗时 {} S".format(time.time() - s))

这个没什么看的,下一个


线程池与多线程爬虫

import requests
import time
from concurrent.futures import ThreadPoolExecutor
import threading

List_Url = ['https://img.soutula.com/large/006APoFYly8hbhsbsmciuj30hs0hfaam.jpg',
            'https://img.soutula.com/large/ceeb653ely8hbhrnv8vx1j20hs0hj3zo.jpg',
            'https://img.soutula.com/large/006APoFYly8hbhm20mb6rj30hs0hmt9j.jpg',
            'https://img.soutula.com/large/ceeb653ely8hbhixn8cvvj205k0563yf.jpg',
            'https://img.soutula.com/large/ceeb653ely8hbhbzwzy4ej20hs0f1753.jpg']

def run(Url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36 Edg/91.0.864.53',
        'Referer': 'https://www.fabiaoqing.com/biaoqing'
    }
    response = requests.get(Url,headers=headers)
    with open("爬fabiaoqing网图/" + Url[-8:-4] + '.jpg', 'wb') as w:
        content =response.content
        w.write(content)

if __name__ == '__main__':
    s = time.time()

    with ThreadPoolExecutor(max_workers=5) as pool:
        # for Url in List_Url:
        #     pool.submit(run,Url)
        pool.map(run,List_Url)

    # threads = []
    # for url in List_Url:
    #     thread = threading.Thread(target=run, args=(url,))
    #     thread.start()
    #     threads.append(thread)
    # [j.join() for j in threads]
    print("总耗时 {} S".format(time.time() - s))

一般来说,爬虫最常用的就是它两了,效率上其实影响不大,主要还是跟当前的响应什么的挂钩。

值得注意的是用法上的区别。


协程

import asyncio
import time
import aiohttp

List_Url = ['https://img.soutula.com/large/006APoFYly8hbhsbsmciuj30hs0hfaam.jpg',
            'https://img.soutula.com/large/ceeb653ely8hbhrnv8vx1j20hs0hj3zo.jpg',
            'https://img.soutula.com/large/006APoFYly8hbhm20mb6rj30hs0hmt9j.jpg',
            'https://img.soutula.com/large/ceeb653ely8hbhixn8cvvj205k0563yf.jpg',
            'https://img.soutula.com/large/ceeb653ely8hbhbzwzy4ej20hs0f1753.jpg']

async def run(session, Url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36 Edg/91.0.864.53',
        'Referer': 'https://www.fabiaoqing.com/biaoqing'
    }
    async with session.get(Url,headers=headers) as response:
        with open("爬fabiaoqing网图/" + Url[-8:-4] + '.jpg', 'wb') as w:
            content = await response.content.read()
            w.write(content)

async def main():
    async with aiohttp.ClientSession() as session:
        tasks = [asyncio.create_task(run(session, Url)) for Url in List_Url]
        await asyncio.wait(tasks)

if __name__ == '__main__':
    s = time.time()
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

    print("总耗时 {} S".format(time.time() - s))

对于协程不懂的就看看基础,再来吧,此处的并发协程,速率还是有比较的明显的提升的。网络波动不大的情况下,是要比多线程要快的。


线程池协程异步

「小小的说一句,此处并没有做任何验证,只是单纯的测试了一下」如果不对欢迎指出。

import asyncio
import time
import aiohttp
from concurrent.futures import ThreadPoolExecutor

List_Url = ['https://img.soutula.com/large/006APoFYly8hbhsbsmciuj30hs0hfaam.jpg',
            'https://img.soutula.com/large/ceeb653ely8hbhrnv8vx1j20hs0hj3zo.jpg',
            'https://img.soutula.com/large/006APoFYly8hbhm20mb6rj30hs0hmt9j.jpg',
            'https://img.soutula.com/large/ceeb653ely8hbhixn8cvvj205k0563yf.jpg',
            'https://img.soutula.com/large/ceeb653ely8hbhbzwzy4ej20hs0f1753.jpg']

async def run(session, Url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36 Edg/91.0.864.53',
        'Referer': 'https://www.fabiaoqing.com/biaoqing'
    }
    async with session.get(Url, headers=headers) as response:
        with open("爬fabiaoqing网图/" + Url[-8:-4] + '.jpg', 'wb') as w:
            content = await response.content.read()
            w.write(content)

async def main():
    async with aiohttp.ClientSession() as session:
        tasks = [asyncio.create_task(run(session, Url)) for Url in List_Url]
        await asyncio.wait(tasks)

if __name__ == '__main__':
    s = time.time()

    loop = asyncio.get_event_loop()
    with ThreadPoolExecutor(max_workers=5) as pool:
        pool.submit(loop.run_until_complete(main()))

    print("总耗时 {} S".format(time.time() - s))

小结

上述代码中,请忽略用法上的冗余!

最后: 下方这份完整的软件测试视频学习教程已经整理上传完成,朋友们如果需要可以自行免费领取【保证100%免费】

在这里插入图片描述

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值