scrapy文件管道
安装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文件管道的更多相关文章
- scrapy之管道
scrapy之管道 通过管道将数据持久化到数据库中,企业中常见的数据库是MySQL,分布式爬取数据时只能讲数据存储到Redis装,还可以将数据存储到本地磁盘(即写入到本地文件中). 未完待续... 0
- scrapy 图片管道学习笔记
使用scrapy首先需要安装 python环境使用3.6 windows下激活进入python3.6环境 activate python36 mac下 mac@macdeMacBook-Pro:~$ ...
- python文件管道 下载图集
# -*- coding: utf-8 -*- import re from time import sleep import scrapy from scrapy.linkextractors im ...
- Scrapy框架——安装以及新建scrapy文件
一.安装 conda install Scrapy :之后在按y 表示允许安装相关的依赖库(下载速度慢的话也可以借助镜像源),安装的前提是安装了anaconda作为python , 测试scr ...
- 爬虫框架Scrapy 之(二) --- scrapy文件
框架简介 核心部分: 引擎.下载器.调度器 自定义部分: spider(自己建的爬虫文件).管道(pipelines.py) 目录结构 firstSpider firstSpider spiders ...
- scrapy学习---管道
使用管道必须实现process_item() 方法 process_item(self, item, spider) 次方法实现数据的过滤处理等操作 open_spider(self, spider) ...
- Scrapy学习篇(九)之文件与图片下载
Media Pipeline Scrapy为下载item中包含的文件(比如在爬取到产品时,同时也想保存对应的图片)提供了一个可重用的 item pipelines . 这些pipeline有些共同的方 ...
- scrapy保存csv文件有空行的解决方案
比如现在我有一个名为test的爬虫,运行爬虫后将结果保存到test.csv文件 默认情况下,我执行scrapy crawl test -o test.csv ,得到的结果可能就是下面这种情况,每两行中 ...
- scrapy框架--新建调试的main.py文件
一.原因: 由于pycharm中没有scrapy的一个模板,所有没办法直接在scrapy文件中调试,所有我们需要写一个自己的main.py文件,在文件里面调用命令行,来实现scrapy的一个调试.(在 ...
随机推荐
- JDBC连接抽象方法的使用例子
package com.zdlt.auth.api.base.datasource; import java.sql.*; import java.util.ArrayList; import jav ...
- jquery 回车键 调用tab 事件
$(function(){ $("input").keydown(function(){ == event.keyCode){ var form = $("body&qu ...
- SDN实验---Ryu的应用开发(二)Learning Switch
一:自学习交换机(二层MAC交换机)的编程思路 (一)明确问题 如何实现软件定义的自学习交换机? (二)设计解决方案 通过控制器来实现自学习交换算法,然后指导数据平面实现交换机操作 (三)确定具体的技 ...
- matlab学习笔记12_2创建结构体数组,访问标量结构体,访问非标量结构体数组的属性,访问嵌套结构体中的数据,访问非标量结构体数组中多个元素的字段
一起来学matlab-matlab学习笔记12 12_2 结构体 创建结构体数组,访问标量结构体,访问非标量结构体数组的属性,访问嵌套结构体中的数据,访问非标量结构体数组中多个元素的字段 觉得有用的话 ...
- 从原理到应用,Elasticsearch详解
简介 Elasticsearch(简称ES)是一个分布式.可扩展.实时的搜索与数据分析引擎.ES不仅仅只是全文搜索,还支持结构化搜索.数据分析.复杂的语言处理.地理位置和对象间关联关系等. ES的底层 ...
- springboot docker 部署
1.新建一个最简单的springboot项目 https://code.aliyun.com/859143303/hello-world.git 2.src/main/docker下新建Dockerf ...
- Linux CentOS7 下无图形界面安装Oracle11G R2版本
01,系统 Centos7 数据库版本 Oracle_11gR2 ,以及硬件要求 内存不能小于 1G,可用硬盘不小于8G Swap分区空间不小于2G grep MemTotal /proc/memin ...
- CentOS下安装Tomcat
CentOS版本:CentOS-7-x86_64-Minimal-1810 1.安装JDK 详情查看:CentOS下安装JDK-rpm文件.CentOS安装JDK-tar.gz文件 2.下载tomca ...
- 【C++】static关键字有哪些用法?其主要作用是什么?
static关键字的用法: 1)将全局变量修饰为静态全局变量 存储在静态存储区,整个程序运行期间一直存在 静态全局变量在声明它的文件之外是不可见的,只要声明它的文件可见,而普通的全局变量则是所有文件可 ...
- CentOS7-Docker 搭建Maven私服
使用Docker搭建Maven私服 前言本文主要介绍,使用Docker创建一个nexus私服,然后编写一个Library,上传到私服,然后使用demo工程依赖. 本文不对Maven.Nexus.私服等 ...