我无法从网页上找到特定的标题

2024-04-18 17:45:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用scrapy从amazon网站上抓取数据,当我使用selector gadget显示包含title类的路径时,它不会提取该标题。相反,当我对类使用{.s-access-title}时,它就工作了。我不知道为什么选择器小工具显示了错误的路径。你知道吗

import scrapy
from ..items import AmazonsItem


class AmazonSpiderSpider(scrapy.Spider):
    name = 'amazon_spider'
    start_urls = \['https://www.amazon.in/s?k=agatha+christie+books&crid=3MWRDVZPSKVG0&sprefix=agatha%2Caps%2C269&ref=nb_sb_ss_i_1_6'\]

    def parse(self, response):

        items =  AmazonsItem()

        product_name = response.css('.s-access-title').extract()][1]

amazon page 如果你看这个图片,我只选择标题,但它有不同的类,它不工作,当我使用这个类。 那么如何从中提取一个特定的类标题呢? 如果你有经验,选择小工具,请看看。 如果有人对如何提取有其他想法,请告诉我。你知道吗


Tags: 工具数据nameimport路径标题amazonaccess
1条回答
网友
1楼 · 发布于 2024-04-18 17:45:42

试试这个:标题在data-attribute

import scrapy
from ..items import AmazonsItem

class AmazonSpiderSpider(scrapy.Spider):
    name = 'amazon_spider'
    start_urls = ['https://www.amazon.in/s?k=agatha+christie+books&crid=3MWRDVZPSKVG0&sprefix=agatha%2Caps%2C269&ref=nb_sb_ss_i_1_6']

    def parse(self, response):
        items =  AmazonsItem()
        products_name = response.css('.s-access-title::attr("data-attribute")').extract()
        for product_name in products_name:
            print(product_name)
        next_page = response.css('li.a-last a::attr(href)').get()
            if next_page is not None:
                next_page = response.urljoin(next_page)
                yield scrapy.Request(next_page, callback=self.parse)

输出:

'Murder on the Orient Express (Poirot)'
'And Then There Were None'
.
.

相关问题 更多 >