python 爬虫系列09-异步斗图来一波
斗图斗图,妈妈再也不怕我都不赢了
import requests
from lxml import etree
from urllib import request
import os
import re
from queue import Queue
import threading class Procuder(threading.Thread):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36'
}
def __init__(self,page_queue, img_queue, *args, **kwargs):
super(Procuder, self).__init__(*args, **kwargs)
self.page_queue = page_queue
self.img_queue = img_queue def run(self):
while True:
if self.page_queue.empty():
break
url = self.page_queue.get()
self.parse_page(url) def parse_page(self, url):
reponse = requests.get(url, headers=self.headers)
text = reponse.text
html = etree.HTML(text)
imgs = html.xpath("//div[@class='page-content text-center']//img[@class!='gif']")
for img in imgs:
img_url = img.get('data-original')
alt = img.get('alt')
alt = re.sub(r'[\?\.\/\"<>:?!,!\*]','',alt)
suffix = os.path.splitext(img_url)[1]
filename = alt + suffix
filename = re.sub(r'!dta', '', filename)
self.img_queue.put((img_url,filename)) class Consumer(threading.Thread):
def __init__(self, page_queue, img_queue, *args, **kwargs):
super(Consumer, self).__init__(*args, **kwargs)
self.page_queue = page_queue
self.img_queue = img_queue def run(self):
while True:
if self.img_queue.empty() and self.page_queue.empty():
break
img_url, filename = self.img_queue.get()
request.urlretrieve(img_url, 'image2/'+filename)
print(filename+' 下载完成!!') def main():
page_queue = Queue(100)
img_queue = Queue(1000)
for x in range(1, 200):
url = 'http://www.doutula.com/photo/list/?page=%d' % x
page_queue.put(url)
for x in range(5):
t = Procuder(page_queue, img_queue)
t.start()
for x in range(5):
t = Consumer(page_queue, img_queue)
t.start()
if __name__ == '__main__':
main()
python 爬虫系列09-异步斗图来一波的更多相关文章
- python 爬虫系列08-同步斗图一波
一波大图来袭 import requests from lxml import etree from urllib import request import os import re def par ...
- Python爬虫入门教程 13-100 斗图啦表情包多线程爬取
斗图啦表情包多线程爬取-写在前面 今天在CSDN博客,发现好多人写爬虫都在爬取一个叫做斗图啦的网站,里面很多表情包,然后瞅了瞅,各种实现方式都有,今天我给你实现一个多线程版本的.关键技术点 aioht ...
- 深夜,我用python爬取了整个斗图网站,不服来斗
QQ.微信斗图总是斗不过,索性直接来爬斗图网,我有整个网站的图,不服来斗. 废话不多说,选取的网站为斗图啦,我们先简单来看一下网站的结构 网页信息 从上面这张图我们可以看出,一页有多套图,这个时候我们 ...
- Python爬虫入门教程 2-100 妹子图网站爬取
妹子图网站爬取---前言 从今天开始就要撸起袖子,直接写Python爬虫了,学习语言最好的办法就是有目的的进行,所以,接下来我将用10+篇的博客,写爬图片这一件事情.希望可以做好. 为了写好爬虫,我们 ...
- python爬虫系列(2)—— requests和BeautifulSoup
本文主要介绍python爬虫的两大利器:requests和BeautifulSoup库的基本用法. 1. 安装requests和BeautifulSoup库 可以通过3种方式安装: easy_inst ...
- python 爬虫系列教程方法总结及推荐
爬虫,是我学习的比较多的,也是比较了解的.打算写一个系列教程,网上搜罗一下,感觉别人写的已经很好了,我没必要重复造轮子了. 爬虫不过就是访问一个页面然后用一些匹配方式把自己需要的东西摘出来. 而访问页 ...
- $python爬虫系列(2)—— requests和BeautifulSoup库的基本用法
本文主要介绍python爬虫的两大利器:requests和BeautifulSoup库的基本用法. 1. 安装requests和BeautifulSoup库 可以通过3种方式安装: easy_inst ...
- Python爬虫系列 - 初探:爬取旅游评论
Python爬虫目前是基于requests包,下面是该包的文档,查一些资料还是比较方便. http://docs.python-requests.org/en/master/ POST发送内容格式 爬 ...
- Python爬虫系列(七):提高解析效率
如果仅仅因为想要查找文档中的<a>标签而将整片文档进行解析,实在是浪费内存和时间.最快的方法是从一开始就把<a>标签以外的东西都忽略掉. SoupStrainer 类可以定义文 ...
随机推荐
- 【转】android中如何实现离线缓存
原文地址:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1209/2136.html 离线缓存就是在网络畅通的情况下将从服务器收 ...
- 构建空Datatable
方法一 DT = new DataTable(); DataRow DR = DT.NewRow(); //构建列(名称,数据类型) DT.Columns.Add("代码", Ty ...
- .NET Core Api 集成 swagger
废话不多讲 第一步 当然是要通过 NuGet 安装第三方插件 swagger 程序包管理器控制台,安装命令:Install-Package Swashbuckle.AspNetCore -Pre 第 ...
- 苹果开发者账号提示“Unable to verify mobile phone number”的解决方案
在注册苹果开发者账号时,会提示:"Unable to verify mobile phone number.".顾名思义,没有有效的手机号码. 解决方案: 进入到Your Appl ...
- NSLocale 本地化信息
前言 NSLocale 类返回本地化信息,主要体现在"语言"和"区域格式"这两个设置项. 1.NSLocale 本地化信息的创建 // 用标示符创建 NSLoc ...
- iOS App 内部跳转(设置、Wifi、蓝牙...)关键词
1.iOS 10 以前: 蜂窝网络:prefs:root=MOBILE_DATA_SETTINGS_ID Wi-Fi:prefs:root=WIFI 定位服务:prefs:root=LOCATION_ ...
- 「BZOJ 2733」「HNOI 2012」永无乡「启发式合并」
题意 你需要维护若干连通快,有两个操作 合并\(x,y\)所在的连通块 询问\(x\)所在连通块中权值从小到大排第\(k\)的结点编号 题解 可以启发式合并\(splay\),感觉比较好些的 一个连通 ...
- go 递归实现快排
package main import ( "fmt" ) func main() { arr := []int{1,2,5,8,7,4,3,6,9,0,12,13,45,78,8 ...
- avascript的匿名函数
一.什么是匿名函数? 在Javascript定义一个函数一般有如下三种方式: 函数关键字(function)语句: function fnMethodName(x){alert(x);} 函数字面量( ...
- Sublime的一些问题记录
1.默认的中文字体显示异常(参考:关于最新版的Sublime Text 3中默认的中文字体样式显示异常的问题) 1. 选择Perferences(首选项)--> settings(设置) 2. ...