【问题标题】:I'm not able to scrape particular title from a website我无法从网站上抓取特定标题
【发布时间】:2019-04-16 17:13:03
【问题描述】:

我正在使用 scrapy 从亚马逊网站抓取数据,当我使用选择器小工具显示具有标题类的路径时,它不会提取该标题。相反,当我将 {.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 如果你看这张图片,我只选择了标题,但它有不同的类,当我使用这个类时它不起作用。 那么如何从中提取特定的类标题呢? 如果您有使用选择器小工具的经验,请查看。 另外,如果有人对如何提取它有其他想法,请告诉。

【问题讨论】:

    标签: python scrapy css-selectors


    【解决方案1】:

    试试这个:标题在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'
    .
    .
    

    【讨论】:

    • 那么为什么选择器小工具给了我一个不同的类?因为我找不到 .s-access-title。我也想要其他信息,这就是我问的原因。
    • 因为它会动态呈现数据。仔细检查response.text,你会发现不同。
    • attr("data-attribute")...为什么会这样?
    • 另外,请告诉我如何知道数据是动态创建的?这里的动态是什么意思?
    • 动态由于您通过检查窗口观察的网页与通过某些脚本的原始响应略有不同。这是原始标签:```

      死亡约会 (Poirot)

      ``` 并且渲染的标签是:``` 死亡约会(波洛) ```
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 2021-09-27
    • 1970-01-01
    相关资源
    最近更新 更多