【问题标题】:Can´t download images with Scrapy无法使用 Scrapy 下载图像
【发布时间】:2017-05-02 16:51:16
【问题描述】:

我无法下载图片。我有几个问题(我尝试了很多变化)。这是我的代码(我猜它有很多错误)

目标是抓取起始 URL 并保存所有产品图片并通过 SKU 编号更改其名称。此外,蜘蛛必须单击“下一步按钮”才能在所有页面中执行相同的任务(大约有 24.000 个产品)

我注意到的问题是:

  1. 我不知道 Items Pipelines 的确切配置
  2. 图片不会下载到 settings.py 的文件夹中
  3. 我想按分辨率过滤图像并使用缩略图。推荐的配置是哪一种?
  4. 图像位于另一台服务器上。这是个问题?

SETTINGS.PY

BOT_NAME = 'soarimages'

SPIDER_MODULES = ['soarimages.spiders']
NEWSPIDER_MODULE = 'soarimages.spiders'
DEFAULT_ITEM_CLASS = 'soarimages.items'
ITEM_PIPELINES = {'soarimages.pipelines.soarimagesPipeline': 1}
IMAGES_STORE = '/soarimages/images'

ITEMS.PY

import scrapy

class soarimagesItem(scrapy.Item):
    title = scrapy.Field()
    image_urls = scrapy.Field()
    images = scrapy.Field()

PIPELINES.PY

import scrapy
from scrapy.contrib.pipeline.images import ImagesPipeline

class soarimagesPipeline(ImagesPipeline):

def set_filename(self, response):
    #add a regex here to check the title is valid for a filename.
    return 'full/{0}.jpg'.format(response.meta['title'][0])

def get_media_requests(self, item, info):
    for image_url in item['image_urls']:
        yield scrapy.Request(image_url, meta={'title': item['title']})

def get_images(self, response, request, info):
    for key, image, buf in super(soarimagesPipeline, self).get_images(response, request, info):
        key = self.set_filename(response)
    yield key, image, buf

Productphotos.PY(蜘蛛)

# import the necessary packages
import scrapy
from scrapy.spiders import Rule, CrawlSpider
from scrapy.linkextractors import LinkExtractor
from soarimages.items import soarimagesItem

class soarimagesSpider(scrapy.Spider):
name = 'productphotos'
allowed_domains = ['http://sodimac.com.ar','http://sodimacar.scene7.com']
start_urls = ['http://www.sodimac.com.ar/sodimac-ar/search/']
rules = [Rule(LinkExtractor(allow=['http://sodimacar.scene7.com/is/image//SodimacArgentina/.*']), 'parse')]

def parse(self, response):
    SECTION_SELECTOR = '.one-prod'
    for soarimages in response.css(SECTION_SELECTOR):
        image = soarimagesItem()
        image['title'] = response.xpath('.//p[@class="sku"]/text()').re_first(r'SKU:\s*(.*)').strip(),
        rel = response.xpath('//div/a/img/@data-original').extract_first()
        image['image_urls'] = ['http:'+rel[0]]
        yield image

    NEXT_PAGE_SELECTOR = 'a.next ::attr(href)'
    next_page = response.css(NEXT_PAGE_SELECTOR).extract_first()
    if next_page:
        yield scrapy.Request(
            response.urljoin(next_page),
            callback=self.parse
        )

【问题讨论】:

    标签: python web-scraping scrapy


    【解决方案1】:

    这是我的代码(我猜它有很多错误)

    事实上,我至少可以发现一个错误:allowed_domains 应该只列出域。不得包含任何 http:// 前缀:

    allowed_domains = ['sodimac.com.ar', 'sodimacar.scene7.com']
    

    您可能想解决这个问题并测试您的蜘蛛。如果出现新问题,请为每个特定问题创建特定问题。这样可以更轻松地帮助您。另见how to ask

    【讨论】:

    • 非常感谢@Frank。问题已经解决了!无论如何,我无法将图像存储在我的计算机中!我指定了路径“IMAGES_STORE = '/Users/NicolasParraga/Desktop/soarimages/soarimages/images'”但没有用。有什么解决办法吗?
    • 您正在覆盖 ImagePipeline。虽然这对于实现额外的功能非常有用,但它使调试变得更加困难。因此,也许您想从标准的 ImagePipeline 开始,并注意任何指示任何问题的日志消息。如果您不理解日志消息,请发布一个新问题,包括代码和日志。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    • 2018-12-15
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多