优雅的用异步进程实现效率爬虫

爬虫进阶

主要用到了进程池来加速

  • 代码在这儿啦,相信优秀的你看代码就会懂了。
  • 谨慎运行( = = !)
  • 其中 PIL 模块 不存在的话就 win+R 打开cmd 装一下模块命令如下:
  • pip install Pillow -i https://pypi.douban.com/simple/
from PIL import Image
from io import BytesIO
import requests
import time
from multiprocessing import Pool


def get_res():
    pool = Pool(10)  # 每次打开图片的数量(进程数量)
    while True:
        time.sleep(5)  # 每次打开图片间隔 10秒
        try:
            xs = pool.apply_async(func=show_img)
        except Exception as e:
            print("请求超时" + str(e))


def show_img():
    url = "https://api.66mz8.com/api/rand.tbimg.php"
    res = requests.get(url).content
    b = BytesIO(res)
    img = Image.open(b)
    img.resize((50, 50), Image.ANTIALIAS)
    img.show()


if __name__ == '__main__':
    get_res()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值