【问题标题】:Unable to scrape product title from Amazon无法从亚马逊抓取产品标题
【发布时间】:2020-05-24 04:50:11
【问题描述】:

我正在使用 Scrapy 在this Amazon website 上获取产品的价格和标题。提取价格没有问题,但我对标题有疑问。不同之处在于我在类属性中看到“aria-hidded=true”。这是一个例子。

<div class="p13n-sc-truncated" aria-hidden="true" data-rows="2" title="Igloo ICEB26HNAQ Automatic Self-Cleaning Portable Electric Countertop Ice Maker Machine With Handle, 26 Pounds in 24 Hours, 9 Ice Cubes Ready in 7 minutes, With Ice Scoop and Basket">Igloo ICEB26HNAQ Automatic Self-Cleaning Portable Electric Countertop Ice Maker Machine…</div>

这里是css选择器命令:

title = response.css('.p13n-sc-truncated').css('::text').extract()

我可以知道提取文本的 CSS 选择器应该是什么。谢谢

【问题讨论】:

    标签: python css scrapy


    【解决方案1】:

    您可以通过 XPATH 解决它。 转到 xpather 并将您的 html 发送到那里并提取您的 xpath 模式。

    import scrapy
    from scrapy import Spider
    class SSDSpider(scrapy.Spider):
        name = "SSD_spider"
        start_urls = ['https://www.amazon.com/Best-Sellers-Appliances/zgbs/appliances/ref=zg_bs_nav_0']
        DOWNLOAD_DELAY = 10
        def parse(self, response):
            yield {
                    'title': response.xpath('//div[@class="p13n-sc-truncated"][1]').extract(),
                  }
    

    尝试使用美丽的汤:

    pip install beautifulsoup4
    pip install lxml 
    apt-get install python-lxml
    

    Beautiful Soup 也依赖一个解析器,默认是 lxml

    import bs4 as bs
    import urllib.request
    
    source = urllib.request.urlopen('https://your_amazon_link/product/').read()
    soup = bs.BeautifulSoup(source,'lxml')
    for title in soup.select("ol#zg-ordered-list > li"):
        title_name = title.select_one(".p13n-sc-truncated").get_text()
        print(title_name)
    

    【讨论】:

    • 感谢您的帮助。我正在使用scrapy构建解决方案,因此如果有scrapy的建议,我将不胜感激。谢谢
    • 没有。这是我在 stackoverflow 上的第一篇文章。
    • 我的意思是你的链接,你会从中获得标题。
    • 是的,它是最畅销页面的产品之一
    【解决方案2】:

    如果您查看 html 源代码 (ctrl + u),您会发现产品标题确实有另一个类 p13n-sc-line-clamp-2,它工作得非常好。所以你的 css-selector 可能看起来像这样:

    response.css('.p13n-sc-line-clamp-2::text').get().strip()
    

    这是一个最小的工作示例:

    from scrapy.spiders import CrawlSpider
    
    class amaSpider(CrawlSpider):
        name = 'amatitle'
        start_urls = ['https://www.amazon.com/Best-Sellers-Appliances/zgbs/appliances/']
    
        def parse(self, response):
            yield{'title': response.css('.p13n-sc-line-clamp-2::text').get().strip()}
    

    如果您想提取所有标题并将它们从前导和尾随空格中删除,请将 parse-function 更改为以下内容:

        def parse(self, response):
            titles = response.css('.p13n-sc-line-clamp-2::text').getall()
            titles_strip = [x.strip() for x in titles]
            yield{'titles': titles_strip}
    

    【讨论】:

    • 代码按预期工作。添加了一个最小的最小工作示例。
    • @_carao_jo 我试过了,仍然为空,你能发送结果中的屏幕截图吗?
    • @MahsaHassankashi 给你:i.stack.imgur.com/F9AVr.png
    【解决方案3】:

    你的代码很好:

    >>> from parsel import Selector
    >>> selector = Selector(text='<div class="p13n-sc-truncated" aria-hidden="true" data-rows="2" title="Igloo ICEB26HNAQ Automatic Self-Cleaning Portable Electric Countertop Ice Maker Machine With Handle, 26 Pounds in 24 Hours, 9 Ice Cubes Ready in 7 minutes, With Ice Scoop and Basket">Igloo ICEB26HNAQ Automatic Self-Cleaning Portable Electric Countertop Ice Maker Machine…</div>')
    >>> selector.css('.p13n-sc-truncated').css('::text').extract()
    ['Igloo ICEB26HNAQ Automatic Self-Cleaning Portable Electric Countertop Ice Maker Machine…']
    

    我的猜测是响应不包含预期的 HTML。如果这是亚马逊,那是极有可能的。他们有相当多的反机器人措施。

    【讨论】:

      猜你喜欢
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-03
      • 2020-04-11
      • 1970-01-01
      • 2017-08-11
      相关资源
      最近更新 更多