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. mac php7.2 安装mcrypt扩展

    安装: brew install libmcrypt 下载mcrypt扩展源码 http://pecl.php.net/package/mcrypt 解压后 进入目录: phpize ./config ...

  2. Mac 命令行查看自己的公网IP

    Mac 查看本机公网IP 命令 curl ifconfig.me 如果想更好看一点 curl ipinfo.io/json 还可以用wget wget http://ipecho.net/plain ...

  3. [转载]IBM公司发布了最新的power7服务器p750 p770 p780

    [转载]IBM公司发布了最新的power7服务器p750 p770 p780 (2015-06-11 12:54:17) 转载▼ http://blog.sina.com.cn/s/blog_6f52 ...

  4. Python设计模式之MVC模式

    # -*- coding: utf-8 -*- # author:baoshan quotes = ('A man is not complete until he is married. Then ...

  5. CVAE-GAN论文学习-1

    CVAE-GAN: Fine-Grained Image Generation through Asymmetric Training 摘要 我们提出了一个变分生成对抗网络,一个包含了与生成对抗网络结 ...

  6. IsNull、rs、sum

    <% 'response.write (IsNull(rs("month_finish_count_price"))) If IsNull(rs("month_fi ...

  7. Opencv拉普拉斯算子做图像增强

    Opencv拉普拉斯算子——图像增强 #include <iostream> #include <opencv2/opencv.hpp> using namespace std ...

  8. python for 无限循环

    class Infinit: def __iter__(self): return self def __next__(self): return None for i in Infinit(): p ...

  9. Spring Boot系列之-helloword入门

    一. What: Spring Boot是什么?以1.4.3.RELEASE为例,官方介绍为:http://docs.spring.io/spring-boot/docs/1.4.3.RELEASE/ ...

  10. 【Leetcode_easy】993. Cousins in Binary Tree

    problem 993. Cousins in Binary Tree 参考 1. Leetcode_easy_993. Cousins in Binary Tree; 完