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. CRM 线索 客户 统称为 资源 客户服务管理篇 销售易

    线索 客户 统称为 资源 - 国内版 Binghttps://cn.bing.com/search?FORM=U227DF&PC=U227&q=%E7%BA%BF%E7%B4%A2+% ...

  2. c语言字符串分割函数(转)

    源:C语言实现split以某个字符分割一个字符串 void split(char *src, const char *separator, char **dest, int *num) { /* sr ...

  3. Python不带参数的类装饰器

    # -*- coding: utf-8 -*- # author:baoshan # 不带参数的类装饰器 # 类装饰器的实现,必须实现__call__和__init__两个内置函数. # __init ...

  4. html两端对齐的代码

    html语言两端对齐的代码为: <p style="text-align:justify; text-justify:inter-ideograph;"> 文字,需要对 ...

  5. ES6深入浅出-6 ES 6 模块-1.模块化速学

    把模块先默认认为是豆腐块 为什么前端需要模块? 没有模块的方式 预览这个html页面     一共200行js代码 前100行在做一件事 ,另外100行在做另一件事,这样就是两个模块 main.js来 ...

  6. 邪淫真正的可怕危害 (转自学佛网:http://www.xuefo.net/nr/article54/544414.html)

    邪淫真正的可怕危害 邪淫的害处可能很快就显现,也可能是逐渐的表现出来.但往往后者的害处更大.因为当积累了多年的邪淫果报一旦显现,后悔就已经晚了. 我本人就是一个很好的例子.回想自己这40多年的日子,邪 ...

  7. 细聊Oracle通过ODBC数据源连接SQL Server数据库

    类似文章搜索引擎上有很多,内容大致相同,今天所谓细聊是因为我在借鉴这些文章时候走了些弯路,所以写此文,为自己备忘,同时如果能为初涉此处知识点的小伙伴提供些帮助就更好了,文章结尾处的一些扩展有一定实战意 ...

  8. chrome浏览器调试JS代码

    是怎么调试 JavaScript 程序的?最原始的方法是用 alert() 在页面上打印内容,稍微改进一点的方法是用 console.log() 在 JavaScript 控制台上输出内容.嗯~,用这 ...

  9. 【opencv基础-VxWorks】话说图像格式转换-COLOR_YUV2BGR_YUY2

    前言 基于Vxworks的WindRiver获取摄像头图像进行处理,需要先进行转换,对于转换格式博主有点疑问.本文对此作出解释,若有错误,请交流指正. README.md The video came ...

  10. jquery 单击选中 再次选中取消选中

    html: <div id="full" class='weui-popup__container' style="background: #fff"&g ...