python使用多线程爬取表情包

使用多线程爬取数据可以显著提高效率

编辑环境:pycharm
目标:爬取表情包库的所有表情包
首先在同目录下建一个images文件夹
这里写图片描述
代码如下

#coding:utf8

import os
import threading
import requests
import urllib
from bs4 import BeautifulSoup

base_page_url = 'https://www.doutula.com/photo/list/?page='
#页面url列表
page_url_list = []
#表情url列表
face_url_list = []
#全局锁
glock = threading.Lock()
for x in range(1,870):
    url = base_page_url + str(x)
    page_url_list.append(url)

def procuder():
    while True:
        glock.acquire()
        if len(page_url_list) == 0:
            glock.release()
            break
        else:
            page_url = page_url_list.pop()
            glock.release()
            response = requests.get(page_url)
            content = response.content
            soup = BeautifulSoup(content, 'lxml')
            img_list = soup.find_all('img', attrs={'class': 'img-responsive lazy image_dta'})
            glock.acquire()
            for img in img_list:
                url = img['data-original']
                if not url.startswith('http'):
                    url = 'http:' + url
                face_url_list.append(url)
            glock.release()

def customer():
    while True:
        glock.acquire()
        if len(face_url_list)==0:
            glock.release()
            continue
        else:
            face_url = face_url_list.pop()
            glock.release()
            split_list = face_url.split('/')
            filename = split_list.pop()
            path = os.path.join('images', filename)
            urllib.urlretrieve(face_url, filename=path)

def main():
    #创建4个多线程作为生产者,爬取图片
    for x in range(4):
        th = threading.Thread(target=procuder)
        th.start()
    #创建5个多线程作为消费者,下载图片
    for x in range(5):
        th = threading.Thread(target=customer)
        th.start()

if __name__ =='__main__':
    main()

一共4w多张表情包,美滋滋
一共4w多张表情包,美滋滋

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值