scrapy 图片管道学习笔记
使用scrapy首先需要安装
python环境使用3.6
windows下激活进入python3.6环境
activate python36
mac下
mac@macdeMacBook-Pro:~$ source activate python36
(python36) mac@macdeMacBook-Pro:~$
安装 scrapy
(python36) mac@macdeMacBook-Pro:~$ pip install scrapy
(python36) mac@macdeMacBook-Pro:~$ scrapy --version
Scrapy 1.8. - no active project Usage:
scrapy <command> [options] [args] Available commands:
bench Run quick benchmark test
fetch Fetch a URL using the Scrapy downloader
genspider Generate new spider using pre-defined templates
runspider Run a self-contained spider (without creating a project)
settings Get settings values
shell Interactive scraping console
startproject Create new project
version Print Scrapy version
view Open URL in browser, as seen by Scrapy [ more ] More commands available when run from project directory Use "scrapy <command> -h" to see more info about a command
(python36) mac@macdeMacBook-Pro:~$ scrapy startproject images
New Scrapy project 'images', using template directory '/Users/mac/anaconda3/envs/python36/lib/python3.6/site-packages/scrapy/templates/project', created in:
/Users/mac/images You can start your first spider with:
cd images
scrapy genspider example example.com (python36) mac@macdeMacBook-Pro:~$ cd images
(python36) mac@macdeMacBook-Pro:~/images$ scrapy genspider -t crawl pexels www.pexels.com
Created spider 'pexels' using template 'crawl' in module:
images.spiders.pexels
(python36) mac@macdeMacBook-Pro:~/images$
setting.py里面 关闭robot.txt遵循
ROBOTSTXT_OBEY = False
分析目标网站规则 www.pexels.com
https://www.pexels.com/photo/man-using-black-camera-3136161/
https://www.pexels.com/video/beach-waves-and-sunset-855633/
https://www.pexels.com/photo/white-vehicle-2569855/
https://www.pexels.com/photo/monochrome-photo-of-city-during-daytime-3074526/
得出要抓取的规则
rules = (
Rule(LinkExtractor(allow=r'^https://www.pexels.com/photo/.*/$'), callback='parse_item', follow=True),
) 图片管道 要定义两个item
class ImagesItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
image_urls = scrapy.Field()
images = scrapy.Field()
images_url是抓取到的图片url 需要传递过来
images 检测图片完整性,但是我打印好像没看到这个字段
pexels.py里面引入item 并且定义对象
# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule from images.items import ImagesItem class PexelsSpider(CrawlSpider):
name = 'pexels'
allowed_domains = ['www.pexels.com']
start_urls = ['http://www.pexels.com/'] rules = (
Rule(LinkExtractor(allow=r'^https://www.pexels.com/photo/.*/$'), callback='parse_item', follow=False),
) def parse_item(self, response):
item = ImagesItem()
item['image_urls'] = response.xpath('//img[contains(@src,"photos")]/@src').extract()
print(item['image_urls'])
return item
设置setting.py里面启用图片管道 设置存储路劲
ITEM_PIPELINES = {
#'images.pipelines.ImagesPipeline': ,
'scrapy.pipelines.images.ImagesPipeline':
}
IMAGES_STORE = '/www/crawl'
# 图片的下载地址 根据item中的字段来设置哪一个内容需要被下载
IMAGES_URLS_FIELD = 'image_urls'
启动爬虫
scrapy crawl pexels --nolog
发现已经下载下来了

但是下载的图片不是高清的,要处理下图片的后缀
setting.py打开默认管道 设置优先级高一些
ITEM_PIPELINES = {
'images.pipelines.ImagesPipeline': 1,
'scrapy.pipelines.images.ImagesPipeline': 2
}
管道文件里面对后缀进行处理去掉
class ImagesPipeline(object):
def process_item(self, item, spider):
tmp = item['image_urls']
item['image_urls'] = []
for i in tmp:
if '?' in i:
item['image_urls'].append(i.split('?')[0])
else:
item['image_urls'].append(i) return item
最终下载的就是大图了,但是图片管道还是默认对图片会有压缩的,所以如果使用文件管道下载的才是完全的原图,非常大。
如果不下载图片,直接存图片url到mysql的话参考
https://www.cnblogs.com/php-linux/p/11792393.html
图片管道 配置最小宽度和高度分辨率
IMAGES_MIN_HEIGHT=800
IMAGES_MIN_WIDTH=600
IMAGES_EXPIRES=90 天 不会对重复的进行下载
生成缩略图
IMAGES_THUMBS={
‘small’:(50,50),
'big':(600,600)
}
scrapy 图片管道学习笔记的更多相关文章
- scrapy爬虫框架学习笔记(一)
scrapy爬虫框架学习笔记(一) 1.安装scrapy pip install scrapy 2.新建工程: (1)打开命令行模式 (2)进入要新建工程的目录 (3)运行命令: scrapy sta ...
- css 3 背景图片为渐变色(渐变色背景图片) 学习笔记
6年不研究CSS发现很多现功能都没有用过,例如渐变色,弹性盒子等,年前做过一个简单的管理系统,由于本人美工不好,设计不出好看的背景图片,偶然百度到背景图片可以使用渐变色(感觉发现了新大陆).以后的项目 ...
- Scrapy 爬虫框架学习笔记(未完,持续更新)
Scrapy 爬虫框架 Scrapy 是一个用 Python 写的 Crawler Framework .它使用 Twisted 这个异步网络库来处理网络通信. Scrapy 框架的主要架构 根据它官 ...
- Angular2之管道学习笔记
管道.可以把一个输出流与另一个输入流连接起来.类似 linux.gulp都有应用. 在Angular2中使用管道非常方便.Angular2中本身提供了一些内置管道.当然也可以自定义管道. 文档链接:h ...
- Scrapy:学习笔记(2)——Scrapy项目
Scrapy:学习笔记(2)——Scrapy项目 1.创建项目 创建一个Scrapy项目,并将其命名为“demo” scrapy startproject demo cd demo 稍等片刻后,Scr ...
- scrapy 学习笔记1
最近一段时间开始研究爬虫,后续陆续更新学习笔记 爬虫,说白了就是获取一个网页的html页面,然后从里面获取你想要的东西,复杂一点的还有: 反爬技术(人家网页不让你爬,爬虫对服务器负载很大) 爬虫框架( ...
- 机器学习框架ML.NET学习笔记【6】TensorFlow图片分类
一.概述 通过之前两篇文章的学习,我们应该已经了解了多元分类的工作原理,图片的分类其流程和之前完全一致,其中最核心的问题就是特征的提取,只要完成特征提取,分类算法就很好处理了,具体流程如下: 之前介绍 ...
- 机器学习框架ML.NET学习笔记【7】人物图片颜值判断
一.概述 这次要解决的问题是输入一张照片,输出人物的颜值数据. 学习样本来源于华南理工大学发布的SCUT-FBP5500数据集,数据集包括 5500 人,每人按颜值魅力打分,分值在 1 到 5 分之间 ...
- Redis学习笔记7--Redis管道(pipeline)
redis是一个cs模式的tcp server,使用和http类似的请求响应协议.一个client可以通过一个socket连接发起多个请求命令.每个请求命令发出后client通常会阻塞并等待redis ...
随机推荐
- Transaction 那点事儿
Transaction 那点事儿 https://my.oschina.net/huangyong/blog/160012
- react问题You must install peer dependencies yourself.
npm WARN react-native@0.46.4 requires a peer of react@16.0.0-alpha.12 but none is installed. You mus ...
- ospf的路由更新和撤销总结
首先ospf 的报文有:hello报文,主要作用ospf 邻居建立及维护.dd报文,主要作用主从选举,序列号主从的确认,mtu的协商(可选).lsr 报文,主要作用向邻居请求lsa.lsu报文,主要作 ...
- spring cloud java: 无法访问redis.clients.jedis.JedisPoolConfig 找不到redis.clients.jedis.JedisPoolConfig的类文件
spring cloud <spring-cloud.version>Greenwich.SR3</spring-cloud.version> 注入Redis 时候: @Bea ...
- 【KakaJSON手册】07_Coding_归档_解档
KakaJSON可以只用一行代码将常用数据进行归档\解档 后面代码中会用到 file 文件路径 // 文件路径(String或者URL都可以) let file = "/Users/mj/D ...
- axios 设置headers token
axios({ method:"put", url:"....", data:{"action":"refreshToken&qu ...
- SQL 对比,移动累计
数据对比 两种常用模型 1.对比下一行,判断增长.减少.维持现状 -- 建表 drop table sales create table sales( num int, soc int ); inse ...
- [Linux.centOS].安装Redis 腾讯云
环境 { "服务器运营商":"腾讯云", "操作系统":"CentOS 7.5 64位", "CPU" ...
- 使用Cloud Toolkit部署SpringBoot项目到服务器
由于我们经常发布项目到测试服,在测试服上调试一些本地无法调试的东西,所以出现了各种打包,然后上传.启动,时间都耗费在这无聊的事情上面了,偶然在网上看到IntelliJ IDEA有 Cloud Tool ...
- Win10 专业版 Hyper-V 主机计算服务无法启动
Windows 10升级1809版本后,发现Hyper-V不能用了,管理器里是一片空白,看服务Hyper-V 主机计算服务没有启动,手动启动的话失败,报错,代码1053. 自己尝试修复,也百度了很久, ...