有下载图像问题

发布于 2025-02-13 10:08:28 字数 1481 浏览 0 评论 0原文

我已经建立了一个简单的刮刀来从网站下载图像。不幸的是,我在下载这些图像时遇到了问题,因此没有下载任何内容。我已经在线搜索了类似的问题,并练习了这些问题,但对我不起作用。我过去曾做过这项工作,所以我不明白为什么它现在不起作用。

我的刮板:

import scrapy
from scrapy_exercises.items import ScrapyExercisesItem


class TestSpider(scrapy.Spider):
    name = 'test'
    start_urls = ['https://www.meadowhall.co.uk/eatdrinkshop?page=1']

    def start_requests(self):
        for url in self.start_urls:
            yield scrapy.Request(
                url=url,
                callback=self.parse
            )

    def parse(self, response):

        content_page = response.xpath("//div[@class='view-content']//div")
        for cnt in content_page:

            link = cnt.xpath('.//a/@href').get()
            image_url = cnt.xpath(".//img//@src").get()
            
            if link != None:
                items = ScrapyExercisesItem()
                items['images'] = [image_url.split('?')[0]]
                yield items

pipelines.py

from scrapy.pipelines.images import ImagesPipeline
class DownfilesPipeline(ImagesPipeline):
    def file_path(self, request, response=None, info=None):
        image_name: str = request.url.split("/")[-1]
        return image_name

settings.py

ITEM_PIPELINES = {
    'scrapy_exercises.pipelines.DownfilesPipeline': 55
    }
IMAGES_STORE = '.'

items.py:


class ScrapyExercisesItem(scrapy.Item):
    images = scrapy.Field()

I have built a simple scraper to download images from a website. Unfortunately, I am having issues with downloading these images such that nothing gets downloaded. I have searched online for similar issues, and have practiced these but it does not work for me. I have had this work in the past, so I cannot understand why it does not work now.

My scraper:

import scrapy
from scrapy_exercises.items import ScrapyExercisesItem


class TestSpider(scrapy.Spider):
    name = 'test'
    start_urls = ['https://www.meadowhall.co.uk/eatdrinkshop?page=1']

    def start_requests(self):
        for url in self.start_urls:
            yield scrapy.Request(
                url=url,
                callback=self.parse
            )

    def parse(self, response):

        content_page = response.xpath("//div[@class='view-content']//div")
        for cnt in content_page:

            link = cnt.xpath('.//a/@href').get()
            image_url = cnt.xpath(".//img//@src").get()
            
            if link != None:
                items = ScrapyExercisesItem()
                items['images'] = [image_url.split('?')[0]]
                yield items

pipelines.py

from scrapy.pipelines.images import ImagesPipeline
class DownfilesPipeline(ImagesPipeline):
    def file_path(self, request, response=None, info=None):
        image_name: str = request.url.split("/")[-1]
        return image_name

settings.py

ITEM_PIPELINES = {
    'scrapy_exercises.pipelines.DownfilesPipeline': 55
    }
IMAGES_STORE = '.'

items.py:


class ScrapyExercisesItem(scrapy.Item):
    images = scrapy.Field()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清音悠歌 2025-02-20 10:08:29

我认为您需要做的就是添加一些设置,并在您的项目类中包含一个

字段

class ScrapyExercisesItem(scrapy.Item):
    images = scrapy.Field()
    results = scrapy.Field()

结果

IMAGES_URLS_FIELD = 'images'
IMAGES_RESULT_FIELD = 'results'

I think all you need to do is add a few settings and include a results field in your item class

In your items.py file add this:

class ScrapyExercisesItem(scrapy.Item):
    images = scrapy.Field()
    results = scrapy.Field()

then in your settings.py file add this:

IMAGES_URLS_FIELD = 'images'
IMAGES_RESULT_FIELD = 'results'

Then try it again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文