scrapy 下载图片 from cuiqingcai
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
DOWNLOADER_MIDDLEWARES = {

scrapy 下载图片 from cuiqingcai的更多相关文章
- scrapy下载图片到自己的目录,创建缩略图,存储入库
环境和工具:python2.7,scrapy 实验网站:http://www.27270.com/tag/333.html 爬去所有兔女郎图片,下面的推荐需要过滤 逻辑:分析网站信息,下载图片和入库 ...
- Scrapy下载图片及自定义分类下载路径
配置下载图片的流程如下 在items中定义两个属性,image_urls 和images .image_urls是用来存储需要下载的图片url链接,列表类型: 当文件下载完成后会把相关下载信息存入im ...
- 利用scrapy下载图片保存到本地
1.先声明一下,起始位置已经是将所有的图片链接都能到pipelines.py中 2.创建一个类,继承于ImagesPipeline,因此也就需要导入ImagesPipeline from scrapy ...
- [转]解决scrapy下载图片时相对路径转绝对路径的问题
专注自:http://blog.csdn.net/hjy_six/article/details/6862648 这段时间一直在研究利用scrapy抓取图片的问题,我发觉,用官网的http://doc ...
- Scrapy 下载图片时 ModuleNotFoundError: No module named'PIL'
使用scrapy的下载模块需要PIL(python图像处理模块)的支持,使用pip安装即可
- Scrapy 下载图片
参考 : https://www.jianshu.com/p/6c8d2730d088 https://docs.scrapy.org/en/latest/topics/item-pipeline.h ...
- scrapy下载图片报[scrapy.downloadermiddlewares.robotstxt] DEBUG: Forbidden by robots.txt:错误
本文转自:http://blog.csdn.net/zzk1995/article/details/51628205 先说结论,关闭scrapy自带的ROBOTSTXT_OBEY功能,在setting ...
- Day3-scrapy爬虫下载图片自定义名称
学习Scrapy过程中发现用Scrapy下载图片时,总是以他们的URL的SHA1 hash值为文件名,如: 图片URL:http://www.example.com/image.jpg 它的SHA1 ...
- 用Scrapy爬虫下载图片(豆瓣电影图片)
用Scrapy爬虫的安装和入门教程,这里有,这篇链接的博客也是我这篇博客的基础. 其实我完全可以直接在上面那篇博客中的代码中直接加入我要下载图片的部分代码的,但是由于上述博客中的代码已运行,已爬到快九 ...
随机推荐
- 我的Chrome插件
1.AdBlock 用来屏蔽广告,用过的人都说好. 2.Flash Block(Plus) 用来限制Flash的播放. 3.Flash Control 用来限制Flash的播放. 4.Full Pag ...
- windows server 2008 R2如何更换系统界面语言/中文换英文
下面我们来讲解一下如何将中文系统转化成日文.韩文.英文等其它语言界面的系统. 以windows server 2008 R2系统中文变英文为例: 1.到微软官方下载:Windows Server 20 ...
- DataFrame.nunique(),DataFrame.count()
1. nunique() DataFrame.nunique(axis = 0,dropna = True ) 功能:计算请求轴上的不同观察结果 参数: axis : {0或'index',1或'co ...
- php curl POST multipart/form-data与application/x-www-form-urlencode的区别
背景 CURL在 a.php 中以 POST方式向 b.php 提交数据,但b.php无法接收到数据,而 CURL 操作显示成功. 原来,"传递一个数组到CURLOPT_POSTFIELDS ...
- python repr和str
都是将对象转换为字符串 repr """ repr(object) -> string Return the canonical string representa ...
- (已解决)Xcode 运行报错: clang: error: unknown argument: '-websockets'
报错内容: 解决办法:
- mybatis之接口绑定
接口绑定方案 mybatis中,提供了一套接口绑定方案,程序员可以提供一个接口,然后提供对应接口的一个mapper.xml文件.MyBatis会自动将接口和xml文件进行绑定.实际上就是mybatis ...
- !! MACD战法总结
我现在只发技术,不预测大盘.其实说实话,大盘不用预测,只要按照guoweijohn战法,有买入信号就入,有卖出信号就出..你也会成为股神..不是吹牛,且听慢慢分解 股市有三种市场: 一.牛市 二.震荡 ...
- 字符串转化为int数组
String a = "1,2,3,4,5,6" String str[] = a.split(","); int array[] = new int[str. ...
- POJ 2155 Matrix(二维BIT)
Matrix [题目链接]Matrix [题目类型]二维BIT &题解: bit只能单点更新,恰好,这题可以想一下就可以用单点更新解决了. 只不过最后我交上去居然T了,想了10多分钟,试了一下 ...