我尝试在python中使用scrapy刮特征图像,但它给出了“无”,事实上,我尝试了3到4种刮特征图像的方法

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

我试图刮特征图像使用在Python中的scrapy,但它给'无'事实上,我已经尝试了3至4方法刮,但他们都不工作。谁能请帮助我为什么我的任何代码不给图像的源链接,提前感谢。
这是准则。

class NewsSpider(scrapy.Spider):
    name = "cruisefever"

    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):
        Feature_Image = response.xpath('//*[@id="td_uid_2_634abd2257025"]/div/div[1]/div/div[8]/div/p[1]/img/@data-src').extract()[0]
        #Feature_Image = response.xpath('//*[@id="td_uid_2_634abd2257025"]/div/div[1]/div/div[8]/div/p[1]/img/@data-img-url').extract()[0]
        #Feature_Image = response.xpath('//*[@id="td_uid_2_634abd2257025"]/div/div[1]/div/div[8]/div/p[1]/img/@src').extract()[0]
        #Feature_Image = [i.strip() for i in response.css('img[class*="alignnone size-full wp-image-39164 entered lazyloaded"] ::attr(src)').getall()][0]

        yield{
            'Feature_Image': Feature_Image,
        }

这里是网站链接https://cruisefever.net/carnival-cruise-lines-oldest-ship-sailing-final-cruise/

ws51t4hk

ws51t4hk1#

您可以使用此xpath来抓取专题图像,

class NewsSpider(scrapy.Spider):
    name = "cruisefever"

    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):
        image_tag = response.xpath('//div[@id="tdb-autoload-article"]/div/div/article/div/div/div/div/div/div//img')[1]
        Feature_Image = image_tag.attrib['src']

        yield{
            'Feature_Image': Feature_Image,
        }

相关问题