scrapy 之 4K美食图片下载

编程入门 行业动态 更新时间:2024-10-26 02:27:12

scrapy 之 4K美食<a href=https://www.elefans.com/category/jswz/34/1753570.html style=图片下载"/>

scrapy 之 4K美食图片下载

这里不说废话了直接上代码

 

# -*- coding: utf-8 -*-
import scrapy
from .. items import ImagenetItemclass ImageSpider(scrapy.Spider):name = 'image'allowed_domains = ['picbian']# 请求最开始的urlstart_urls = ['/']def parse(self, response):# 根据响应(response)来找到指定的内容   现在找到的是img的src属性img_list = response.xpath('//ul[@class="clearfix"]/li/a/img/@src')print(img_list)# 找到了很多src属性值   现在进行遍历   分别使用每一个for img in img_list:# 使用在items.py中创建的数据模型itemitem = ImagenetItem()# 拼接url   得到完整的下载地址src = '' + img.extract()# 将得到的下载地址   放入到数据模型中# item['src'] = src# 下载地址要包在列表当中item['src'] = [src]# 将数据模型传输给管道yield itemnext_url = response.xpath('//div[@class="page"]/a[text()="下一页"]/@href').extract()# print('---------------------------------------------')# print(next_url)if len(next_url) != 0:print('----------------')url = '' + next_url[0]# 将url传给scrapy.Request   得到的结果继续用self.parse进行处理yield scrapy.Request(url=url,callback=self.parse)

items.py

# -*- coding: utf-8 -*-# Define here the models for your scraped items
#
# See documentation in:
# .htmlimport scrapyclass ImagenetItem(scrapy.Item):# define the fields for your item here like:# name = scrapy.Field()src = scrapy.Field()pass

settings.py

# -*- coding: utf-8 -*-# Scrapy settings for imageNet project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     .html
#     .html
#     .htmlBOT_NAME = 'imageNet'SPIDER_MODULES = ['imageNet.spiders']
NEWSPIDER_MODULE = 'imageNet.spiders'# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'imageNet (+)'# Obey robots.txt rules
# ROBOTSTXT_OBEY = True# Configure maximum concurrent requests performed by Scrapy (default: 16)
#CONCURRENT_REQUESTS = 32# Configure a delay for requests for the same website (default: 0)
# See .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 .html
#SPIDER_MIDDLEWARES = {
#    'imageNet.middlewares.ImagenetSpiderMiddleware': 543,
#}# Enable or disable downloader middlewares
# See .html
#DOWNLOADER_MIDDLEWARES = {
#    'imageNet.middlewares.ImagenetDownloaderMiddleware': 543,
#}# Enable or disable extensions
# See .html
#EXTENSIONS = {
#    'scrapy.extensions.telnet.TelnetConsole': None,
#}# Configure item pipelines
# See .html
ITEM_PIPELINES = {# 'imageNet.pipelines.ImagenetPipeline': 300,#scrapy中  专门负责图片下载的管道'scrapy.pipelines.images.ImagesPipeline':1
}
# 图片的存储路径
IMAGES_STORE = 'imageDownLoad'
# 图片的下载地址  根据item中的字段来设置哪一个内容需要被下载
IMAGES_URL_FIELD = 'src'
# Enable and configure the AutoThrottle extension (disabled by default)
# See .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 .html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

 

更多推荐

scrapy 之 4K美食图片下载

本文发布于:2024-02-07 04:49:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1753494.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图片下载   美食   scrapy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!