python多线程爬取斗图啦数据
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多线程爬取斗图啦数据的更多相关文章
- 爬取斗图网图片,使用xpath格式来匹配内容,对请求伪装成浏览器, Referer 防跨域请求
6.21自我总结 一.爬取斗图网 1.摘要 使用xpath匹配规则查找对应信息文件 将请求伪装成浏览器 Referer 防跨域请求 2.爬取代码 #导入模块 import requests #爬取网址 ...
- python爬取斗图网中的 “最新套图”和“最新表情”
1.分析斗图网 斗图网地址:http://www.doutula.com 网站的顶部有这两个部分: 先分析“最新套图” 发现地址栏变成了这个链接,我们在点击第二页 可见,每一页的地址栏只有后面的pag ...
- Python爬取 斗图表情,让你成为斗图大佬
话不多说,上结果(只爬了10页内容) 上代码:(可直接运行) 用到Xpath #encoding:utf-8 # __author__ = 'donghao' # __time__ = 2018/ ...
- python多线程爬取世纪佳缘女生资料并简单数据分析
一. 目标 作为一只万年单身狗,一直很好奇女生找对象的时候都在想啥呢,这事也不好意思直接问身边的女生,不然别人还以为你要跟她表白啥的,况且工科出身的自己本来接触的女生就少,即使是挨个问遍,样本量也 ...
- Python爬虫爬取豆瓣电影之数据提取值xpath和lxml模块
工具:Python 3.6.5.PyCharm开发工具.Windows 10 操作系统.谷歌浏览器 目的:爬取豆瓣电影排行榜中电影的title.链接地址.图片.评价人数.评分等 网址:https:// ...
- Python 多线程爬取站酷(zcool.com.cn)图片
极速爬取下载站酷(https://www.zcool.com.cn/)设计师/用户上传的全部照片/插画等图片. 项目地址:https://github.com/lonsty/scraper 特点: 极 ...
- 爬虫之爬取豆瓣top250电影排行榜及爬取斗图啦表情包解读及爬虫知识点补充
今日内容概要 如何将爬取的数据直接导入Excel表格 #如何通过Python代码操作Excel表格 #前戏 import requests import time from openpyxl impo ...
- py3+requests+urllib+bs4+threading,爬取斗图图片
实现原理及思路请参考我的另外几篇爬虫实践博客 py3+urllib+bs4+反爬,20+行代码教你爬取豆瓣妹子图:http://www.cnblogs.com/UncleYong/p/6892688. ...
- shell爬取斗图网
#!/bin/bash read -p "请输入要爬取的页面数(默认为10):" page_num page_num=${page_num:-} echo $page_num re ...
随机推荐
- 悟空CRM(基于jfinal+vue+ElementUI的前后端分离的开源CRM系统)
https://www.jfinal.com/share/1591 官网:http://www.5kcrm.com 官网:http://www.72crm.com 论坛:http://bbs.72cr ...
- Flutter 中的路由
Flutter 中的路由通俗的讲就是页面跳转.在 Flutter 中通过 Navigator 组件管理路由导航. 并提供了管理堆栈的方法.如:Navigator.push 和 Navigator.po ...
- Spring Boot与ActiveMQ的集成
Spring Boot对JMS(Java Message Service,Java消息服务)也提供了自动配置的支持,其主要支持的JMS实现有ActiveMQ.Artemis等.本节中,将以Active ...
- 实现不同的项目,用不同的git 账号提交
可以全局配置一个git 账户名和密码,然后在具体项目里单独配置一个账户名和密码 例如: git config --global user.name "winyh" git conf ...
- MySQL 8中使用全文检索示例
首先建议张册测试用的表test,并使用fulltext说明将title和body两列的数据加入全文检索的索引列中: drop table if exists test; create table te ...
- 《MySQL必知必会》学习笔记——第30章 改善性能
本章将付息与MySQL性能有关的某些要点. 30.1 改善性能 数据库管理员把他们生命中的相当一部分时间花在了调整.试验以改善DBMS性能之上.在诊断英勇的滞缓现象和性能问题时,性能不良的数据库(以及 ...
- nodemon运行 提示错误:无法加载文件 C:\Users\gxf\AppData\Roaming\npm\nodemon.ps1,因为在此系统上禁止运行脚本。
nodemon运行 提示错误:无法加载文件 C:\Users\gxf\AppData\Roaming\npm\nodemon.ps1,因为在此系统上禁止运行脚本. 这是你笔记本禁止运行脚本,解决办法 ...
- chrome devTool
在console中访问节点 使用document.querySelectAll()访问元素 使用$0快速访问选中的元素,光标选中的元素,早console中输入$0获取选中元素的dom信息 拷贝 > ...
- .Net Core 程序报错 在上一个操作完成之前,在此上下文上启动了第二个操作。
错误一: 程序完整报错: A second operation started on this context before a previous operation completed. This ...
- Linux(Ubuntu)下的OpenGl的环境安装, 在qt程序中使用opengl库
OpenGl的环境安装 以下参考自:https://blog.csdn.net/wasaiheihei/article/details/52085397 1. 建立基本编译环境 首先不可或缺的,就是编 ...