python多线程爬取斗图啦网的表情数据

使用到的技术点

  • requests请求库
  • re 正则表达式
  • pyquery解析库,python实现的jquery
  • threading 线程
  • queue 队列
'''
斗图啦多线程方式 ''' import requests,time,re,os
from pyquery import PyQuery as jq
from requests.exceptions import RequestException
from urllib import request
# 导入线程类
import threading
# 导入队列类
from queue import Queue
head = {
"User_Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
}
# 创建项目文件夹
pt=os.path.dirname(os.path.abspath(__file__))
path = os.path.join(pt, "斗图啦")
if not os.path.exists(path):
os.mkdir(path) '''
生产者类
继承自多线程类threading.Thread
重写init方法和run方法
'''
class Producer(threading.Thread):
def __init__(self,img_queue,url_queue,*args,**kwargs):
super(Producer, self).__init__(*args,*kwargs)
self.img_queue=img_queue
self.url_queue=url_queue def run(self):
while True:
if self.url_queue.empty():# 如果没有url了 直接退出循环
break
url=self.url_queue.get()
self.parse_page(url)
## 解析数据方法
def parse_page(self,url):
res=requests.get(url,headers=head)
doc=jq(res.text)
# print(res.text)
# 查询到所有的a标签
items= doc.find(".page-content a").items()
for a in items:
title=a.find("p").text()
src=a.find("img.img-responsive").attr("data-original")
# 分割路径 拿到扩展名
pathtype= os.path.splitext(src)[1]
# 使用正则表达式 去掉特殊字符
patitle=re.sub(r'[\.。,\??\*!!\/~]',"",title)
filename = patitle + pathtype
filepath=os.path.join(path,filename)
# 添加到消费者队列 循环下载图片
self.img_queue.put((filepath,src)) '''
消费者
和生产者一样的道理
'''
class Customer(threading.Thread):
def __init__(self,img_queue,url_queue,*args,**kwargs):
super(Customer, self).__init__(*args,**kwargs)
self.img_queue=img_queue
self.url_queue=url_queue def run(self):
while True:
if self.img_queue.empty() and self.url_queue.empty():#如果没有url并且图片下载完成 直接退出
break
# 在队列中拿到路径和图片链接
filepath,src=self.img_queue.get()
print('%s开始下载,链接%s' % (filepath, src))
# 请求图片
img = requests.get(src)
# 写入本地 content表示二进制数据,text是文本数据
with open(filepath, "wb")as f:
f.write(img.content)
# request.urlretrieve(src,os.path.join(path,filename))
print('%s下载完成' % filepath) def main():
# 构建url队列和img队列
url_queue=Queue(100000)
img_queue=Queue(100000) # 构建url 爬取1到100页的数据
for i in range(1,101):
url="https://www.doutula.com/photo/list/?page="+str(i)
url_queue.put(url)# 添加到生产者队列中
# 开启5个线程线程执行生产者
for i in range(5):
t=Producer(img_queue,url_queue)
t.start()
# 开启3个线程线程执行消费者
for i in range(3):
t=Customer(img_queue,url_queue)
t.start() if __name__ == '__main__':
print("爬虫调度启动---------")
main()
print("爬虫调度完成---------")

python多线程爬取斗图啦数据的更多相关文章

  1. 爬取斗图网图片,使用xpath格式来匹配内容,对请求伪装成浏览器, Referer 防跨域请求

    6.21自我总结 一.爬取斗图网 1.摘要 使用xpath匹配规则查找对应信息文件 将请求伪装成浏览器 Referer 防跨域请求 2.爬取代码 #导入模块 import requests #爬取网址 ...

  2. python爬取斗图网中的 “最新套图”和“最新表情”

    1.分析斗图网 斗图网地址:http://www.doutula.com 网站的顶部有这两个部分: 先分析“最新套图” 发现地址栏变成了这个链接,我们在点击第二页 可见,每一页的地址栏只有后面的pag ...

  3. Python爬取 斗图表情,让你成为斗图大佬

    话不多说,上结果(只爬了10页内容) 上代码:(可直接运行)   用到Xpath #encoding:utf-8 # __author__ = 'donghao' # __time__ = 2018/ ...

  4. python多线程爬取世纪佳缘女生资料并简单数据分析

    一. 目标 ​ 作为一只万年单身狗,一直很好奇女生找对象的时候都在想啥呢,这事也不好意思直接问身边的女生,不然别人还以为你要跟她表白啥的,况且工科出身的自己本来接触的女生就少,即使是挨个问遍,样本量也 ...

  5. Python爬虫爬取豆瓣电影之数据提取值xpath和lxml模块

    工具:Python 3.6.5.PyCharm开发工具.Windows 10 操作系统.谷歌浏览器 目的:爬取豆瓣电影排行榜中电影的title.链接地址.图片.评价人数.评分等 网址:https:// ...

  6. Python 多线程爬取站酷(zcool.com.cn)图片

    极速爬取下载站酷(https://www.zcool.com.cn/)设计师/用户上传的全部照片/插画等图片. 项目地址:https://github.com/lonsty/scraper 特点: 极 ...

  7. 爬虫之爬取豆瓣top250电影排行榜及爬取斗图啦表情包解读及爬虫知识点补充

    今日内容概要 如何将爬取的数据直接导入Excel表格 #如何通过Python代码操作Excel表格 #前戏 import requests import time from openpyxl impo ...

  8. py3+requests+urllib+bs4+threading,爬取斗图图片

    实现原理及思路请参考我的另外几篇爬虫实践博客 py3+urllib+bs4+反爬,20+行代码教你爬取豆瓣妹子图:http://www.cnblogs.com/UncleYong/p/6892688. ...

  9. shell爬取斗图网

    #!/bin/bash read -p "请输入要爬取的页面数(默认为10):" page_num page_num=${page_num:-} echo $page_num re ...

随机推荐

  1. Android 关于selector中item顺序的问题

    selector的item从上到下是按照匹配原则来改变状态的,一旦匹配到某个item的状态,就不会继续往下匹配了. https://blog.csdn.net/l403040463/article/d ...

  2. 网关 apache APISIX

    网关 apache - 国内版 Binghttps://cn.bing.com/search?q=%E7%BD%91%E5%85%B3+apache&qs=n&form=QBRE&am ...

  3. 小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_38、源码编译安装Redis4.x

    笔记 2.源码编译安装Redis4.x     简介:使用源码安装Redis4.x和配置外网访问 1.快速安装  https://redis.io/download#installation      ...

  4. Docs-.NET-C#-指南-语言参考-关键字-值类型:struct

    ylbtech-Docs-.NET-C#-指南-语言参考-关键字-值类型:struct 1.返回顶部 1. struct(C# 参考) 2015/07/20 struct 类型是一种值类型,通常用来封 ...

  5. 记一次haproxy反向代理配置

    首先借用一下前辈的话,解释下反向代理是什么? 反向代理:以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客 ...

  6. 全面系统Python3入门+进阶-1-4 Python的缺点

    结束

  7. ip地址分类和网段详解

    IP地址分类/IP地址10开头和172开头和192开头的区别/判断是否同一网段 简单来说在公司或企业内部看到的就基本都是内网IP,ABC三类IP地址里的常见IP段. 每个IP地址都包含两部分,即网络号 ...

  8. Swift4.0复习闭包

    1.闭包的定义和调用: _ = { (param1: Int, param2: Float, param3: Void) -> return_type in // 闭包执行代码 /* ... * ...

  9. 解决no module named setuptools

    To install setuptools on Debian: sudo apt-get install python-setuptools For Python 3.x: sudo apt-get ...

  10. 玩转CONSUL(1)–WATCH机制探究

    1. 前言 consul 经常被用于服务的注册和发现,本文将带你对watch做更深入的探究 2. consul对外暴露了4种通讯接口 2.1 RPC 主要用于内部通讯Gossip/日志分发/选主等 2 ...