import scrapy

class MzituScrapyItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
name = scrapy.Field()
image_urls = scrapy.Field()
url = scrapy.Field()
pass

  

官方的:

https://doc.scrapy.org/en/latest/topics/media-pipeline.html?highlight=item_complete#scrapy.pipelines.images.ImagesPipeline.item_completed

https://doc.scrapy.org/en/latest/topics/media-pipeline.html?highlight=item_complete

没有分类,很难看, 再重写一下ImagesPipeline中的file_path方法!

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
from scrapy import Request
from scrapy.pipelines.images import ImagesPipeline
from scrapy.exceptions import DropItem
import re class MzituScrapyPipeline(ImagesPipeline): def file_path(self, request, response=None, info=None):
"""
:param request: 每一个图片下载管道请求
:param response:
:param info:
:param strip :清洗Windows系统的文件夹非法字符,避免无法创建目录
:return: 每套图的分类目录
"""
item = request.meta['item']
folder = item['name']
folder_strip = strip(folder)
image_guid = request.url.split('/')[-1]
filename = u'full/{0}/{1}'.format(folder_strip, image_guid)
return filename def get_media_requests(self, item, info):
"""
:param item: spider.py中返回的item
:param info:
:return:
"""
for img_url in item['image_urls']:
referer = item['url']
yield Request(img_url, meta={'item': item,
'referer': referer}) def item_completed(self, results, item, info):
image_paths = [x['path'] for ok, x in results if ok]
if not image_paths:
raise DropItem("Item contains no images")
return item # def process_item(self, item, spider):
# return item def strip(path):
"""
:param path: 需要清洗的文件夹名字
:return: 清洗掉Windows系统非法文件夹名字的字符串
"""
path = re.sub(r'[?\\*|“<>:/]', '', str(path))
return path if __name__ == "__main__":
a = '我是一个?\*|“<>:/错误的字符串'
print(strip(a))

  

写一个中间件来处理图片下载的防盗链:

class MeiZiTu(object):

    def process_request(self, request, spider):
'''设置headers和切换请求头
:param request: 请求体
:param spider: spider对象
:return: None
'''
referer = request.meta.get('referer', None)
if referer:
request.headers['referer'] = referer

  

最后一步设置ImagesPipeline的存储目录!

在settings.py中写入:

IMAGES_STORE = 'F:\mzitu\\'

在settings.py中写入以下配置。

# 30 days of delay for images expiration

IMAGES_EXPIRES = 30
 
ITEM_PIPELINES = {

   'mzitu_scrapy.pipelines.MzituScrapyPipeline': 300,
}

DOWNLOADER_MIDDLEWARES = {

   'mzitu_scrapy.middlewares.MeiZiTu': 543,
}
 

scrapy 下载图片 from cuiqingcai的更多相关文章

  1. scrapy下载图片到自己的目录,创建缩略图,存储入库

    环境和工具:python2.7,scrapy 实验网站:http://www.27270.com/tag/333.html  爬去所有兔女郎图片,下面的推荐需要过滤 逻辑:分析网站信息,下载图片和入库 ...

  2. Scrapy下载图片及自定义分类下载路径

    配置下载图片的流程如下 在items中定义两个属性,image_urls 和images .image_urls是用来存储需要下载的图片url链接,列表类型: 当文件下载完成后会把相关下载信息存入im ...

  3. 利用scrapy下载图片保存到本地

    1.先声明一下,起始位置已经是将所有的图片链接都能到pipelines.py中 2.创建一个类,继承于ImagesPipeline,因此也就需要导入ImagesPipeline from scrapy ...

  4. [转]解决scrapy下载图片时相对路径转绝对路径的问题

    专注自:http://blog.csdn.net/hjy_six/article/details/6862648 这段时间一直在研究利用scrapy抓取图片的问题,我发觉,用官网的http://doc ...

  5. Scrapy 下载图片时 ModuleNotFoundError: No module named'PIL'

    使用scrapy的下载模块需要PIL(python图像处理模块)的支持,使用pip安装即可

  6. Scrapy 下载图片

    参考 : https://www.jianshu.com/p/6c8d2730d088 https://docs.scrapy.org/en/latest/topics/item-pipeline.h ...

  7. scrapy下载图片报[scrapy.downloadermiddlewares.robotstxt] DEBUG: Forbidden by robots.txt:错误

    本文转自:http://blog.csdn.net/zzk1995/article/details/51628205 先说结论,关闭scrapy自带的ROBOTSTXT_OBEY功能,在setting ...

  8. Day3-scrapy爬虫下载图片自定义名称

    学习Scrapy过程中发现用Scrapy下载图片时,总是以他们的URL的SHA1 hash值为文件名,如: 图片URL:http://www.example.com/image.jpg 它的SHA1 ...

  9. 用Scrapy爬虫下载图片(豆瓣电影图片)

    用Scrapy爬虫的安装和入门教程,这里有,这篇链接的博客也是我这篇博客的基础. 其实我完全可以直接在上面那篇博客中的代码中直接加入我要下载图片的部分代码的,但是由于上述博客中的代码已运行,已爬到快九 ...

随机推荐

  1. 详解Laravel的目录结构

    Models 目录在哪里? 许多初学者可能都会困惑 Laravel 为什么没有提供 models 目录,官方解释说是因为不同人对 models 这个词的含义看法不同,容易造成歧义,有些开发者认为应用的 ...

  2. gerrit设置默认rebase以及禁用分支

    1.设置默认rebase 在工程配置中设置: 2.禁用分支 Refrance:refs/for/refs/heads/分支名 禁用权限:push  “Block”   (这样就禁止了开发者往这个分支上 ...

  3. vbox 相关

    1.虚拟机vbox 安装mac os 10.12 图文教程: https://www.cnblogs.com/liming2017/p/7566953.html

  4. url传输编码

    首先:协议规范,RFC 1738,定义url地址中不能包含除:0-9,a-zA-Z,- 之外的字符,即,如URL中包含特殊字符,如$-_.+!*’(), 都要编码. 关于为什么如此定义,有如下几个原因 ...

  5. php二维数组搜索

    $ar = array(     2 => array(         'catid' => 2,         'catdir' => 'notice',     ),     ...

  6. 安装程序无法打开注册表项 UNKNOWN\Components\…的简单解决办法(转)

    安装程序无法打开注册表项 UNKNOWN\Components\…的简单解决办法 2018年04月16日 16:41:32 super_star_贤 阅读数:7193   在安装软件时(比如安装SQL ...

  7. MyBatis基础入门《十九》动态SQL(set,trim)

    MyBatis基础入门<十九>动态SQL(set,trim) 描述: 1. 问题 : 更新用户表数据时,若某个参数为null时,会导致更新错误 2. 分析: 正确结果: 若某个参数为nul ...

  8. idea上更新文件到github上

    1.不是最新文件,那么文件颜色就不一样.操作如下: 2.本地提交   .提交文件列表,提交说明,文件前后对比,确定了后就提交 3.推送到github. (1) (2)

  9. github上删除一个项目或者reposity

    1,点击github的头像,选择如下操作. 2.选择要删除的reposity 3.选择settings 4.复制reposity名字,然后下滑鼠标到底部,选择delete this reposity ...

  10. 3.GDScript(1)概览

    GDScript 是上面提到的用于Godot的主要语言.和其他语言相比,它与Godot高度整合,有许多优点: 简单,优雅,设计上为Lua.Python.Squirrel等语言用户所熟悉. 加载和编译速 ...