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, ...
随机推荐
- java循环1
public class f_w { public static void main(String []args) { int a=0; System.out.print("_info__w ...
- Nginx 常见问题
1. CreateFile() "C:\Users\zhang\Desktop\K\My Project\SSL-数字证书\Nginx配置\nginx-1.12.2/conf/nginx.c ...
- DS博客作业01--日期抽象数据类型设计与实现
1.思维导图及学习体会 1.1第一章绪论知识点思维导图 1.2 学习体会 这次博客园和大作业是我在编程学习中的有意义的进步,第一次尝试使用vs,并且通过同学的一些网站的推荐,和热心同学的帮忙,简单学会 ...
- MongoDB启动报错 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability. 【转】
之前MongoDB启动的时候是蛮正常的,不知道后来启动报错了,就把粘贴出来查询了.最后才知道是由于自己不正常的关闭导致的这个情况. --摘录:MongoDB非正常关闭后修复记录 mongod没有后台执 ...
- 「POI2011 R2 Day2」Tree Rotations【线段树合并】
题目链接 [BZOJ] [洛谷] [LOJ] 题解 由于是前序遍历,那么讨论一棵树上的逆序对的情况. 两个节点都在左子树上 两个节点都在右子树上 两个节点分别在不同的子树上. 前两种情况其实也可以归结 ...
- LOJ子序列
题目描述 https://loj.ac/problem/6074 题解 对于子序列的dp,我们可以设置一个dp. 我们设dp[i]表示以i这个字符结尾的子序列个数,转移为dp[i]+=∑dp[k]-d ...
- 20165223《网络对抗技术》Exp3 免杀原理与实践
目录 -- 免杀原理与实践 免杀原理与实践 本次实验任务 基础知识问答 免杀扫描引擎 实验内容 正确使用msf编码器,msfvenom生成jar等文件,veil-evasion,加壳工具,使用shel ...
- Windows系统CredSSP漏洞修复
Windows系统凭证安全支持提供商协议 (CredSSP) 中存在一个严重漏洞,影响所有 Windows 版本,可导致远程攻击者利用 RDP 和 WinRAW 窃取数据并运行恶意代码. 详见:htt ...
- 重学JavaScript - 数组
作者:狐狸家的鱼 GitHub:surRimn 整理自MDN文档 数组 数组是一种类列表对象,长度和元素类型不固定. 描述 访问数组 JavaScript数组的索引是从0开始的,第一个元素的索引为0, ...
- 树莓派中QT实现串口通讯
树莓派中QT实现串口通讯 开发平台为QT 此博客QT使用的为WiringPi驱动 我使用的串口调试助手为 cutecom 先简单说一些开发过程中需要注意的问题 Linux 下设备为 tty ,对应在 ...