Python爬虫入门教程09:多线程爬取表情包图片

前言💨

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。

前文内容💨

Python爬虫入门教程01:豆瓣Top电影爬取

Python爬虫入门教程02:小说爬取

Python爬虫入门教程03:二手房数据爬取

Python爬虫入门教程04:招聘信息爬取

Python爬虫入门教程05:B站视频弹幕的爬取

Python爬虫入门教程06:爬取数据后的词云图制作

Python爬虫入门教程07:腾讯视频弹幕爬取

Python爬虫入门教程08:爬取csdn文章保存成PDF

PS:如有需要 Python学习资料 以及 解答 的小伙伴可以加点击下方链接自行获取
python免费学习资料以及群交流解答点击即可加入

基本开发环境💨

  • Python 3.6
  • Pycharm
  • wkhtmltopdf

相关模块的使用💨

  • re
  • requests
  • concurrent.futures

安装Python并添加到环境变量,pip安装需要的相关模块即可。

一、💥明确需求

现在聊天谁还不发几个表情包?聊天时,表情包是我们重要的工具,更是拉进小伙伴们距离的好帮手,当聊天陷入尴尬境地时,随手一张表情包,让尴尬化为无形

本篇文章就用python批量爬取表情包图片,留以备用
在这里插入图片描述

二、💥网页数据分析

在这里插入图片描述
如图所示斗图网上面的图片数据都包含在 a 标签当中,可以尝试直接请求这个网页,查看response 返回的数据当中是否也含有 图片地址。

import requests


def get_response(html_url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
    }
    response = requests.get(url=html_url, headers=headers)
    return response


def main(html_url):
    response = get_response(html_url)
    print(response.text)


if __name__ == '__main__':
    url = 'https://www.doutula.com/photo/list/'
    main(url)

在输出结果中 ctrl + F 进行搜索。
在这里插入图片描述
这里有一个点想要注意一下,我用python请求网页所给我们返回的结果当中,包含图片url地址是:
data-original="图片url"
data-backup="图片url"

如果想要提取url地址的话,可以用parsel 解析库,或者 re 正则表达式。之前都是使用的parsel,本篇文章就用 正则表达式吧。

urls = re.findall('data-original="(.*?)"', response.text)

💥单页爬取完整代码

import requests
import re


def get_response(html_url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
    }
    response = requests.get(url=html_url, headers=headers)
    return response


def save(image_url, image_name):
    image_content = get_response(image_url).content
    filename = 'images\\' + image_name
    with open(filename, mode='wb') as f:
        f.write(image_content)
        print(image_name)


def main(html_url):
    response = get_response(html_url)
    urls = re.findall('data-original="(.*?)"', response.text)
    for link in urls:
        image_name = link.split('/')[-1]
        save(link, image_name)


if __name__ == '__main__':
    url = 'https://www.doutula.com/photo/list/'
    main(url)

💥多线程爬取全站图片(如果你的内存够大)

在这里插入图片描述
3631页的数据,什么表情都有,嘿嘿嘿

import requests
import re
import concurrent.futures


def get_response(html_url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
    }
    response = requests.get(url=html_url, headers=headers)
    return response


def save(image_url, image_name):
    image_content = get_response(image_url).content
    filename = 'images\\' + image_name
    with open(filename, mode='wb') as f:
        f.write(image_content)
        print(image_name)


def main(html_url):
    response = get_response(html_url)
    urls = re.findall('data-original="(.*?)"', response.text)
    for link in urls:
        image_name = link.split('/')[-1]
        save(link, image_name)


if __name__ == '__main__':
    # ThreadPoolExecutor 线程池的对象
    # max_workers  最大任务数
    executor = concurrent.futures.ThreadPoolExecutor(max_workers=3)
    for page in range(1, 3632):
        url = f'https://www.doutula.com/photo/list/?page={page}'
        # submit  往线程池里面添加任务
        executor.submit(main, url)
    executor.shutdown()
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值