【问题标题】:Scrapy images and save jpg抓取图像并保存 jpg
【发布时间】:2021-02-08 14:03:57
【问题描述】:

我在抓取图像时遇到了一些问题。

Settings.py:

ITEM_PIPELINES = {
   'scrapy.contrib.pipeline.images.FilesPipeline': 1,
}
FILES_STORE = 'D:/0. Documentos/10. GitHub/0. 93Pipe/RealEstatePredictor/Images'
Items.py

导入scrapy

class MagazineCover(scrapy.Item):
    title = scrapy.Field()
    pubDate = scrapy.Field()
    file_urls = scrapy.Field()
    files = scrapy.Field()
Scrapy spider:

imgs = response.css('div.gallery-content.item-gallery__wrapper  img::attr(src)').getall()
    

    for img in imgs:
        yield MagazineCover(title="title", pubDate="02/03/2021", file_urls=[img])

蜘蛛读取了网址,但是我在Settings.py中设置的文件夹是空的

结果

2021-02-04 15:24:17 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.portalinmobiliario.com/venta/departamento/conchali-metropolitana/6055529-julio-montt-salamanca-5935-uda>
{'file_urls': ['https://http2.mlstatic.com/julio-montt-salamanca-5935-D_NQ_NP_2X_760548-MLC44703096172_012021-R.jpg'],
 'pubDate': '02/03/2021',
 'title': 'title'}

感谢您的帮助!!

【问题讨论】:

标签: python web-scraping scrapy


【解决方案1】:

来自documentation

对于图像管道,使用:

ITEM_PIPELINES = {'scrapy.pipelines.images.ImagesPipeline': 1}

IMAGES_STORE = '/path/to/valid/dir'

对于文件管道,使用:

ITEM_PIPELINES = {'scrapy.pipelines.files.FilesPipeline': 1}

FILES_STORE = '/path/to/valid/dir'

没有scrapy.contrib.pipeline.images.FilesPipeline

【讨论】:

  • 嗨!我做了你提到的,但我仍然没有得到scrapy应该保存所有图像的文件夹“完整”。我不知道我是否遗漏了什么,如果你能帮助我,不胜感激!谢谢!
  • 请确保您有一个正确的目录名称来保存图像。如果你在 Windows 上,你有正确的文件路径格式:docs.microsoft.com/en-us/dotnet/standard/io/file-path-formats。您在路径中使用斜杠“/”,但看起来您需要使用反斜杠“\”
【解决方案2】:

按照documentation保存图片需要三处修改

1.For Images Pipeline, use:

    ITEM_PIPELINES = {'scrapy.pipelines.images.ImagesPipeline': 1}

2.For the Images Pipeline, set the IMAGES_STORE setting:
    IMAGES_STORE = '/path/to/valid/dir'

3. In items.py 

class MagazineCover(scrapy.Item):
      image_urls = scrapy.Field()

你的蜘蛛的结尾会改变这个

for img in imgs:
        yield MagazineCover(image_urls=[img])

希望对你有所帮助。

【讨论】:

  • 嗨!我做了你提到的,但我仍然没有得到scrapy应该保存所有图像的文件夹“完整”。我不知道我是否遗漏了什么,如果你能帮助我,不胜感激!谢谢!
  • 我已使用这些设置成功提取图像以提供您的解决方案。你现在面临什么问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-15
  • 1970-01-01
  • 1970-01-01
  • 2013-07-22
相关资源
最近更新 更多