Scrapy框架下载图片
下载图片
- Scrapy框架下载文件(包括图片有自己一套解决方案,比我们直接使用urlretriever更加有优势)
- 避免重新下载最近下载过的文件
- 可以方便的指定文件存储路径
- 可以将下载的图片转换成通过的格式。比如png或者jpg
- 可以方便的生成缩略图
- 可以方便的检测图片的宽和高,确保他们满足最小的限制
- 异步下载,效率非常高
下载图片的Images Pipeline
- 定义好一个Item,然后在这个item中定义两个属性,分别为image_urls以及images。image_urls是用来存储需要下载的文件的url链接,需要给一个列表
- 当文件下载完成后,会把文件下载的相关信息存储到item的images属性中,如下载路径、下载的url和图片验证嘛
- 在配置文件中settings.py中配置ITEM_PIPELNES,这个配置用来设置图片下载路径
- 启动pipeline:在ITEM_PIPELNES中设置:
'scrapy.piplines.images.ImagesPipeline':1
站酷网图片下载
目标网址:https://www.zcool.com.cn/?p=1
爬虫文件zc.py
# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from ..items import ZcoolItem
class ZcSpider(CrawlSpider):
name = 'zc'
allowed_domains = ['zcool.com.cn']
start_urls = ['https://www.zcool.com.cn/?p=1']
rules = (
Rule(LinkExtractor(allow=r'www\.zcool\.com\.cn/\?p=\d+'), follow=True),
Rule(LinkExtractor(allow=r'www\.zcool\.com\.cn/work/\w+=.html'), callback='parse_item'),
)
def parse_item(self, response):
print(response.url)
item = ZcoolItem()
item['image_urls'] = response.xpath('//div[@class="work-show-box js-work-content"]/div/img/@src').getall()
yield item
settings.py
# -*- coding: utf-8 -*-
# Scrapy settings for zcool project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
# https://docs.scrapy.org/en/latest/topics/settings.html
# https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
# https://docs.scrapy.org/en/latest/topics/spider-middleware.html
BOT_NAME = 'zcool'
SPIDER_MODULES = ['zcool.spiders']
NEWSPIDER_MODULE = 'zcool.spiders'
LOG_LEVEL = 'WARNING'
# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'zcool (+http://www.yourdomain.com)'
# Obey robots.txt rules
ROBOTSTXT_OBEY = False
# Configure maximum concurrent requests performed by Scrapy (default: 16)
#CONCURRENT_REQUESTS = 32
# Configure a delay for requests for the same website (default: 0)
# See https://docs.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
#DOWNLOAD_DELAY = 3
# The download delay setting will honor only one of:
#CONCURRENT_REQUESTS_PER_DOMAIN = 16
#CONCURRENT_REQUESTS_PER_IP = 16
# Disable cookies (enabled by default)
#COOKIES_ENABLED = False
# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False
# Override the default request headers:
#DEFAULT_REQUEST_HEADERS = {
# 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
# 'Accept-Language': 'en',
#}
# Enable or disable spider middlewares
# See https://docs.scrapy.org/en/latest/topics/spider-middleware.html
# SPIDER_MIDDLEWARES = {
# 'zcool.middlewares.ZcoolSpiderMiddleware': 543,
#
# }
# Enable or disable downloader middlewares
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
# 'zcool.middlewares.ZcoolDownloaderMiddleware': 543,
#}
# Enable or disable extensions
# See https://docs.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {
# 'scrapy.extensions.telnet.TelnetConsole': None,
#}
# Configure item pipelines
# See https://docs.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
# 'zcool.pipelines.ZcoolPipeline': 300,
# 使用该scrapy异步下载,权重越小等级越高
'scrapy.pipelines.images.ImagesPipeline': 1
}
# Enable and configure the AutoThrottle extension (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED = True
# The initial download delay
#AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False
# Enable and configure HTTP caching (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'
import os
IMAGES_STORE = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'image')
items.py
# -*- coding: utf-8 -*-
# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html
import scrapy
class ZcoolItem(scrapy.Item):
# 必须定义的两个字段
image_urls = scrapy.Field()
image = scrapy.Field()
pass
pipelines.py
:保持默认
middlewares.py
:保持默认