【问题标题】:Scrapy and XPath issue with nested Xpaths嵌套 Xpath 的 Scrapy 和 XPath 问题
【发布时间】:2016-11-23 21:11:25
【问题描述】:

我正在尝试将亚马逊产品读入 scrapy。 使用此 XPath 从随机类别开始:

products = Selector(response).xpath('//div[@class="s-item-container"]')
for product in products:
    item = AmzItem()
    item['title'] = product.xpath('//a[@class="s-access-detail-page"]/@title').extract()[0]
    item['url'] = product.xpath('//a[@class="s-access-detail-page"]/@href').extract()[0]
    yield item

('//div[@class="s-item-container"]') 在一个类别页面上返回带有产品的所有 div - 这是正确的。

现在,我如何获得产品的链接?

// 代表代码中的任何位置 带有@class 的a 应该选择正确的类 但我得到一个:

item['title'] = product.xpath('//a[@class="s-access-detail-page"]/@title').extract()[0] exceptions.IndexError: list index out of range

所以我的匹配这个 XPath 的列表必须是空的 - 但我不明白为什么?

编辑:
HTML 看起来像这样:

<div class="s-item-container" style="height: 343px;">
<div class="a-row a-spacing-base">
    <div class="a-column a-span12 a-text-left">
        <div class="a-section a-spacing-none a-inline-block s-position-relative">
            <a class="a-link-normal a-text-normal" href="https://rads.stackoverflow.com/amzn/click/com/B0105S434A" rel="nofollow noreferrer"><img alt="Product Details" src="http://ecx.images-amazon.com/images/I/41%2BzrAY74UL._AA160_.jpg" onload="viewCompleteImageLoaded(this, new Date().getTime(), 24, false);" class="s-access-image cfMarker" height="160" width="160"></a>
            <div class="a-section a-spacing-none a-text-center">
                <div class="a-row a-spacing-top-mini">
                    <a class="a-size-mini a-link-normal a-text-normal" href="https://rads.stackoverflow.com/amzn/click/com/B0105S434A" rel="nofollow noreferrer">
                        <div class="a-box">
                            <div class="a-box-inner a-padding-mini"><span class="a-color-secondary">See more choices</span></div>
                        </div>
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="a-row a-spacing-mini">
    <div class="a-row a-spacing-none">
        <a class="a-link-normal s-access-detail-page  a-text-normal" title="Harry Potter Gryffindor School Fancy Robe Cloak Costume And Tie (Size S)" href="https://rads.stackoverflow.com/amzn/click/com/B0105S434A" rel="nofollow noreferrer">
            <h2 class="a-size-base a-color-null s-inline s-access-title a-text-normal">Harry Potter Gryffindor School Fancy Robe Cloak Costume And Tie (Size S)</h2>
        </a>
    </div>
    <div class="a-row a-spacing-mini"><span class="a-size-small a-color-secondary">by </span><span class="a-size-small a-color-secondary">Legend</span></div>
</div>
<div class="a-row a-spacing-mini">
    <div class="a-row a-spacing-none"><a class="a-size-small a-link-normal a-text-normal" href="http://www.amazon.com/gp/offer-listing/B0105S434A/ref=sr_1_21_olp?s=pet-supplies&amp;ie=UTF8&amp;qid=1435391788&amp;sr=1-21&amp;keywords=pet+supplies&amp;condition=new"><span class="a-size-base a-color-price a-text-bold">$28.99</span><span class="a-letter-space"></span>new<span class="a-letter-space"></span><span class="a-color-secondary">(1 offer)</span><span class="a-letter-space"></span><span class="a-color-secondary a-text-strike"></span></a></div>
</div>
<div class="a-row a-spacing-none"><span name="B0105S434A">
    <span class="a-declarative" data-action="a-popover" data-a-popover="{&quot;max-width&quot;:&quot;700&quot;,&quot;closeButton&quot;:&quot;false&quot;,&quot;position&quot;:&quot;triggerBottom&quot;,&quot;url&quot;:&quot;/review/widgets/average-customer-review/popover/ref=acr_search__popover?ie=UTF8&amp;asin=B0105S434A&amp;contextId=search&amp;ref=acr_search__popover&quot;}"><a href="javascript:void(0)" class="a-popover-trigger a-declarative"><i class="a-icon a-icon-star a-star-4"><span class="a-icon-alt">3.9 out of 5 stars</span></i><i class="a-icon a-icon-popover"></i></a></span></span>
    <a class="a-size-small a-link-normal a-text-normal" href="https://rads.stackoverflow.com/amzn/click/com/B0105S434A" rel="nofollow noreferrer">48</a>
</div>
</div>

【问题讨论】:

  • 请张贴相关HTML的sn-p。

标签: xpath scrapy


【解决方案1】:

应该是:

# ------------- The dot makes the query relative to product
product.xpath('.//a[@class="s-access-detail-page"]/@title')

【讨论】:

  • 否 - 我仍然收到此版本的空列表。但我添加了我的 HTML 可能有帮助吗?
  • 好的,让我检查一下
  • a@class="s-access-detail-page" 不是div@class="s-item-container" 的孩子.. 这不是很明显吗?
  • 是的。但我不明白为什么它不起作用。只有一个带有 s-access-detail-page 的 a - 所以我不能用 // 或 .// 选择它。这意味着我需要使用整个路径? div/div/a@?我认为 .// 或 // 正是为了避免添加整个路径?
  • 包含选择正确的路径是必要的,但我在 HTML 的另一个区域使用了 .//,现在它可以工作了,谢谢。
【解决方案2】:

//a[@class="s-access-detail-page"] 必须是 class="s-access-detail-page",因为 xpath 使用字符串而不是含义:) 当你有“多类”时,使用 contains 函数

//a[contains(concat(' ', @class, ' '), " s-access-detail-page ")]/@title

【讨论】:

  • 我不得不删除 concat 部分 - 否则我只会收到一个 `exceptions.ValueError: Invalid XPath` - 但现在它似乎正在工作。还有一个问题 - 不确定这是否来自那个 Xpath 或 sth。否则 - 我会继续挖掘。
  • 如果你仔细做引号,可能是xpath实现的问题:(
  • 现在可以在多个领域工作 - 感谢您通过包含为我指明正确的方向。
  • 很好!我很高兴它有帮助
猜你喜欢
  • 1970-01-01
  • 2017-05-11
  • 2016-03-12
  • 2015-10-09
  • 1970-01-01
  • 2017-02-03
  • 2012-09-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多