我尝试在python中使用scrapy来抓取特征图像,但是它给出的结果是“data:image/svg+xml;....“而不是图像源

monwx1rj  于 2022-11-09  发布在  Python
关注(0)|答案(1)|浏览(153)

我尝试在python中使用scrapy来抓取特征图像,但是它给出的结果是“data:image/svg+xml;......“而不是图像源代码,任何人都可以帮助我解决这个问题,并解释我为什么我得到这个"data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg'%20viewBox%3D'0%200%20600%20400'%2F%3E",而不是图像源代码。
下面是我的Code.class NewsSpider(Scrapy.Spider):name =“巡航无线电”

def start_requests(self):
    url = input("Enter the article url: ")

    yield scrapy.Request(url, callback=self.parse_dir_contents)

def parse_dir_contents(self, response):
    try:
        Feature_Image = [i.strip() for i in response.css('img[class*="wp-image-150660 sp-no-webp"] ::attr(src)').getall()][0]
    except IndexError:
        Author = "NULL"
    yield{
        'Feature_Image': Feature_Image,
    }

这是网站的URL。https://cruiseradio.net/new-expedition-ship-delivered-atlas-ocean-voyages/

lo8azlld

lo8azlld1#

因为该特定属性是动态填充的。请改用data-origin-src属性。

Feature_Image = [i.strip() for i in response.css('img[class*="wp-image-150660 sp-no-webp"] ::attr(data-origin-src)').getall()][0]

结果:

{'Feature_Image': 'https://cdn.cruiseradio.net/wp-content/uploads/2022/10/Atlas_Ocean_Voyages-1030x687.jpeg'}

相关问题