Image Pipeline
Image Pipeline
Scrapy 提供了专门下载文件或者图片的Pipeline,下载图片与文件的原理同抓取网页的原理是一样的,所以他们的下载过程支持多线程与异步,十分的高效
Image Pipeline的工作流程
- itemPipeline从item中获取需要下载的数据,通过Request重新放入到项目队列等待调度器调度下载
- 当图片下载完成,另一个组(images)将被更新到结构中,其中包括下载图片的信息,比如下载路径,源抓取地址(从image_urls组获得)和图片的校验码. images列表中的图片顺序将和源image_urls组保持一致.如果某个图片下载失败,将会记录下错误信息,图片也不会出现在images组中
案例
首先在settings中配置图片存放路径
IMAGES_STORE = './images'
在item中定义需要的数据结构
class Images360Item(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
collection = table = "images"
id = scrapy.Field()
title = scrapy.Field()
url = scrapy.Field()
thumb = scrapy.Field()
定义spider与parse
import scrapy
from urllib.parse import urlencode
from scrapy import Request
from images360.images360.items import Images360Item class ImagesSpider(scrapy.Spider):
name = 'images'
allowed_domains = ['images.so.com']
start_urls = ['http://images.so.com/'] def start_requests(self):
data = {'ch': 'photography',
'listtype': 'hot', }
base_url = 'http://images.so.com/zj?'
for page in range(1, self.settings.get('MAX_PAGE_SIZE') + 1):
sn = page * 30
data['sn'] = sn
params = urlencode(data)
url = base_url + params
print(url)
yield Request(url, self.parse) def parse(self, response):
html = json.loads(response.text)
datas = html.get('list', '')
if datas:
for data in datas:
images_item = Images360Item()
images_item['id'] = data.get('imageid', '')
images_item['title'] = data.get('group_title', '')
images_item['url'] = data.get('qhimg_url', '')
images_item['thumb'] = data.get('qhimg_thumb_url', '')
yield images_item
定义项目管道
from scrapy import Request
from scrapy.exceptions import DropItem
from scrapy.pipelines.images import ImagesPipeline class ImagesPipeline(ImagesPipeline): # 将item中的url取出来 通过Request继续放入到调度器中执行
def get_media_requests(self, item, info):
yield Request(item['url']) # request对应的是当前下载对象,该函数用于放回 文件名
def file_path(self, request, response=None, info=None):
url = request.url
print('url============', url)
file_name = url.split('/')[-1]
return file_name # 单个item完成下载时的处理方法
def item_completed(self,results,item,info):
# results为Item对应的下载结果
# print(results)
# [(True, {'url': 'http://p2.so.qhimgs1.com/t01b866193d9b2101de.jpg', 'path': 't01b866193d9b2101de.jpg',
# 'checksum': 'e074b5cbacd22ac38480d84506fedf02'})] image_path = [x['path'] for ok,x in results if ok]
if image_path:
return item
else:
raise DropItem('image download failed')
注:ImagePipeline的优先级别应该比存入数据库的级别高
Image Pipeline的更多相关文章
- redis大幅性能提升之使用管道(PipeLine)和批量(Batch)操作
前段时间在做用户画像的时候,遇到了这样的一个问题,记录某一个商品的用户购买群,刚好这种需求就可以用到Redis中的Set,key作为productID,value 就是具体的customerid集合, ...
- Building the Testing Pipeline
This essay is a part of my knowledge sharing session slides which are shared for development and qua ...
- Scrapy:为spider指定pipeline
当一个Scrapy项目中有多个spider去爬取多个网站时,往往需要多个pipeline,这时就需要为每个spider指定其对应的pipeline. [通过程序来运行spider],可以通过修改配置s ...
- 图解Netty之Pipeline、channel、Context之间的数据流向。
声明:本文为原创博文,禁止转载. 以下所绘制图形均基于Netty4.0.28版本. 一.connect(outbound类型事件) 当用户调用channel的connect时,会发起一个 ...
- 初识pipeline
1.pipeline的产生 从一个现象说起,有一家咖啡吧生意特别好,每天来的客人络绎不绝,客人A来到柜台,客人B紧随其后,客人C排在客人B后面,客人D排在客人C后面,客人E排在客人D后面,一直排到店面 ...
- MongoDB 聚合管道(Aggregation Pipeline)
管道概念 POSIX多线程的使用方式中, 有一种很重要的方式-----流水线(亦称为"管道")方式,"数据元素"流串行地被一组线程按顺序执行.它的使用架构可参考 ...
- SSIS Data Flow 的 Execution Tree 和 Data Pipeline
一,Execution Tree 执行树是数据流组件(转换和适配器)基于同步关系所建立的逻辑分组,每一个分组都是一个执行树的开始和结束,也可以将执行树理解为一个缓冲区的开始和结束,即缓冲区的整个生命周 ...
- Kafka到Hdfs的数据Pipeline整理
作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 找时间总结整理了下数据从Kafka到Hdfs的一些pipeline,如下 1> Kafka ...
- SQL Queries from Transactional Plugin Pipeline
Sometimes the LINQ, Query Expressions or Fetch just doesn't give you the ability to quickly query yo ...
- One EEG preprocessing pipeline - EEG-fMRI paradigm
The preprocessing pipeline of EEG data from EEG-fMRI paradigm differs from that of regular EEG data, ...
随机推荐
- MyBatis 3
MyBatis 3 学习笔记 一.Mybatis 基础知识 1.MyBatis 3编写步骤: 根据mybatis-config.xml配置文件创建一个SqlSessionFactory对象. sql映 ...
- ZooKeeper连接并创建节点以及实现分布式锁操作节点排序输出最小节点Demo
class LockThread implements Runnable { private DistributedLock lock; public LockThread(int threadId, ...
- Go语言中的Struct
一.Go语言中没有像C#.Java一样的Class,只有Struct这样的结构体.Go语言使用type关键字来定义一个类型. 如下: type User struct { Name string Ag ...
- 树莓派3B+(一)
第一步:安装raspbian系统 介绍:Raspbian是为树莓派设计,基于Debian的操作系统,由一个小团队开发.其不隶属于树莓派基金会,但被列为官方支持的操作系统. 下载地址:https://w ...
- [CQOI2009] 中位数
不错的思维题 传送门:$>here<$ 题意:给出一个N的排列,求出其中有多少个连续子段的中位数是b 数据范围:$N \leq 100000$ $Solution$ 先考虑中位数的意义:一 ...
- Vue js 的生命周期(看了就懂)
转自: https://blog.csdn.net/qq_24073885/article/details/60143856 用Vue框架,熟悉它的生命周期可以让开发更好的进行. 首先先看看官网的图, ...
- 如何解决Redis中的key过期问题
最近我们在Redis集群中发现了一个有趣的问题.在花费大量时间进行调试和测试后,通过更改key过期,我们可以将某些集群中的Redis内存使用量减少25%. Twitter内部运行着多个缓存服务.其中一 ...
- MySQL学习基础知识2
1.基础语句 查 select(* | 字段名 | 四则运算 | 聚合函数) from 表名称; 加上as取别名 as可省略 如:select name, (math+english)/2 total ...
- java返回json设置自定义的格式
使用注解@JsonSerialize(using = CustomPriceSerialize.class) 创建自定义的格式化类(可为内部类) /** * 设置默认返回的小数类型(0.01 元) * ...
- django rest framework pagination
REST framework 包含对可定制分页样式的支持.这使你可以将较大的结果集分成单独的数据页面. 分页 API 支持: 以分页链接的形式作为响应内容的一部分. 以分页链接的形式包含在响应的 he ...