安装scrapy

pip install scrapy

新建项目

(python36) E:\www>scrapy startproject fileDownload
New Scrapy project 'fileDownload', using template directory 'c:\users\brady\.conda\envs\python36\lib\site-packages\scrapy\templates\project', created in:
E:\www\fileDownload You can start your first spider with:
cd fileDownload
scrapy genspider example example.com (python36) E:\www>
(python36) E:\www>scrapy startproject fileDownload
New Scrapy project 'fileDownload', using template directory 'c:\users\brady\.conda\envs\python36\lib\site-packages\scrapy\templates\project', created in:
E:\www\fileDownload You can start your first spider with:
cd fileDownload
scrapy genspider example example.com (python36) E:\www>

编辑爬虫提取内容

# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule from fileDownload.items import FiledownloadItem class PexelsSpider(CrawlSpider):
name = 'pexels'
allowed_domains = ['www.pexels.com']
start_urls = ['https://www.pexels.com/photo/white-concrete-building-2559175/'] rules = (
Rule(LinkExtractor(allow=r'/photo/'), callback='parse_item', follow=True),
) def parse_item(self, response):
print(response.url)
url = response.xpath("//img[contains(@src,'photos')]/@src").extract()
item = FiledownloadItem()
try:
item['file_urls'] = url
print("爬取到图片列表 " + url)
yield item
except Exception as e:
print(str(e))

配置item

class FiledownloadItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
file_urls = scrapy.Field()

  

setting.py

启用文件管道

'scrapy.pipelines.files.FilesPipeline':2  文件管道

FILES_STORE=''  //存储路径

item里面

file_urls = scrapy.Field()

files = scrapy.field()

爬虫里面 改为file_urls参数传递到管道

重写文件管道 保存文件名为图片原名

pipelines.php里面 新建自己图片管道,继承图片管道

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

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html from scrapy.pipelines.files import FilesPipeline
class FiledownloadPipeline(object):
def process_item(self, item, spider):
tmp = item['file_urls']
item['file_urls'] = [] for i in tmp:
if "?" in i:
item['file_urls'].append(i.split('?')[0])
else:
item['file_urls'].append(i)
print(item)
return item class MyFilesPipeline(FilesPipeline):
def file_path(self, request, response=None, info=None):
file_path = request.url
file_path = file_path.split('/')[-1]
print("下载图片"+ file_path)
return 'full/%s' % (file_path)

setting.py 改为启用自己文件管道

ITEM_PIPELINES = {
'fileDownload.pipelines.FiledownloadPipeline': 1,
'fileDownload.pipelines.MyFilesPipeline': 2,
#'scrapy.pipelines.files.FilesPipeline':2
}

获取套图

# -*- coding: utf-8 -*-
from time import sleep import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule class AngelSpider(CrawlSpider):
name = 'angel'
allowed_domains = ['angelimg.spbeen.com']
start_urls = ['http://angelimg.spbeen.com/'] base_url = "http://angelimg.spbeen.com"
rules = (
Rule(LinkExtractor(allow=r'^http://angelimg.spbeen.com/ang/\d+$'), callback='parse_item', follow=False),
) def parse_item(self, response):
item = response.meta.get('item',False)
if item:
pass
else:
item = {}
item['files'] = []
item['file_urls'] = []
print(response.url)
img_url = response.xpath('.//div[@id="content"]/a/img/@src').extract_first()
item['file_urls'].append(img_url) # 如果有下一页 请求下一页,没有数据丢回管道
next_url = response.xpath('.//div[@class="page"]//a[contains(@class,"next")]/@href').extract_first() if next_url:
next_url = self.base_url + next_url
yield scrapy.Request(next_url,callback=self.parse_item,meta={'item':item})
else:
print(item)
yield item
def parse_next_response(self,response,):
item = response.meta.get('item')
print(item,response.url)

  

  github地址

https://github.com/brady-wang/spider-fileDownload

  

scrapy文件管道的更多相关文章

  1. scrapy之管道

    scrapy之管道 通过管道将数据持久化到数据库中,企业中常见的数据库是MySQL,分布式爬取数据时只能讲数据存储到Redis装,还可以将数据存储到本地磁盘(即写入到本地文件中). 未完待续... 0

  2. scrapy 图片管道学习笔记

    使用scrapy首先需要安装 python环境使用3.6 windows下激活进入python3.6环境 activate python36 mac下 mac@macdeMacBook-Pro:~$ ...

  3. python文件管道 下载图集

    # -*- coding: utf-8 -*- import re from time import sleep import scrapy from scrapy.linkextractors im ...

  4. Scrapy框架——安装以及新建scrapy文件

    一.安装 conda install Scrapy   :之后在按y 表示允许安装相关的依赖库(下载速度慢的话也可以借助镜像源),安装的前提是安装了anaconda作为python ,   测试scr ...

  5. 爬虫框架Scrapy 之(二) --- scrapy文件

    框架简介 核心部分: 引擎.下载器.调度器 自定义部分: spider(自己建的爬虫文件).管道(pipelines.py) 目录结构 firstSpider firstSpider spiders ...

  6. scrapy学习---管道

    使用管道必须实现process_item() 方法 process_item(self, item, spider) 次方法实现数据的过滤处理等操作 open_spider(self, spider) ...

  7. Scrapy学习篇(九)之文件与图片下载

    Media Pipeline Scrapy为下载item中包含的文件(比如在爬取到产品时,同时也想保存对应的图片)提供了一个可重用的 item pipelines . 这些pipeline有些共同的方 ...

  8. scrapy保存csv文件有空行的解决方案

    比如现在我有一个名为test的爬虫,运行爬虫后将结果保存到test.csv文件 默认情况下,我执行scrapy crawl test -o test.csv ,得到的结果可能就是下面这种情况,每两行中 ...

  9. scrapy框架--新建调试的main.py文件

    一.原因: 由于pycharm中没有scrapy的一个模板,所有没办法直接在scrapy文件中调试,所有我们需要写一个自己的main.py文件,在文件里面调用命令行,来实现scrapy的一个调试.(在 ...

随机推荐

  1. leetcode No.242 有效的字母异位词 valid-anagram (Python3实现)

    题目描述 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词. 示例 1: 输入: s = "anagram", t = "nagaram&q ...

  2. 解决IDEA中导入新的maven依赖后Language Level自动重置问题

    问题: 弄了个测试项目,因为有涉及JDK1.8的代码,所以将IDEA中默认的Language Level 5(即对应JDK1.5),修改为了8(即对应JDK1.8),但是每次引入新的maven依赖,自 ...

  3. Python的Colorama模块

    简介 Python的Colorama模块,可以跨多终端,显示字体不同的颜色和背景,只需要导入colorama模块即可,不用再每次都像linux一样指定颜色. 1. 安装colorama模块 1 pip ...

  4. DevExpress XtraReport - 动态加载报表布局模板

    XtraReport的报表模板文件是.repx,下面的代码演示动态加载报表布局模板. XtraReport mReport = new XtraReport(); mReport.LoadLayout ...

  5. mongo的用户角色配置

    mongo角色说明: Read:允许用户读取指定数据库 readWrite:允许用户读写指定数据库 dbAdmin:允许用户在指定数据库中执行管理函数,如索引创建.删除,查看统计或访问system.p ...

  6. IntelliJ IDEA重新打开后把字母隐藏怎么办

    默认Font的字体melno不行,r会隐形 解决方案:更换idea字体,点击apply按钮即可正常显示 修改Font为的consoleas  Size:16   line spacing:1.25

  7. jmeter Dashboard Report

    说明:详情参考:https://jmeter.apache.org/usermanual/generating-dashboard.html JMeter3.0以后引入了Dashboard Repor ...

  8. 手撕面试官系列(四 ):MongoDB+Redis 面试专题

    MongoDB   (面试题+答案领取方式见侧边栏) 你说的 NoSQL 数据库是什么意思?NoSQL 与 RDBMS 直接有什么区别?为什么要使用和不使用NoSQL 数据库?说一说 NoSQL 数据 ...

  9. C++ 工程师养成 每日一题third (子数列排序)

    题目: 定义排序子序列为一个数组中一段连续的子序列,并且这段子序列是非递增或者非递减排序的.牛牛有一个长度为n的整数数组A,他现在有一个任务是把数组A分为若干段排序子序列,牛牛想知道他最少可以把这个数 ...

  10. RWMutex:共享/专有的递归互斥锁

    具有共享/独占访问权限,且具有升级/降级功能的互斥锁 介绍 我的目标是创建可以充当读/写锁定机制的对象.任何线程都可以锁定它以进行读取,但是只有一个线程可以锁定它以进行写入.在写入线程释放它之前,所有 ...