Xpath:获取包含其他标签的标签内的文本

我试图在xpath的范围内获得价格。由于它包含一个跨度,因此我得到的唯一值是\ n:

HTML:

<span class="price--default is--nowrap is--discount">
                <span class="price--label">Unser Preis:</span> 19,95&nbsp;€
        </span>

尝试通过xpath获得价值:

>>> selector.xpath(".//span[contains(@class, 'price--default')]/text()").extract_first()
'\n'

与以下同级尝试:

>>> selector.xpath(".//span[contains(@class, 'price--default')]/following-sibling::text()").extract_first()
'\n'

如何使用span标签获取价格?

评论
  • 你太小
    你太小 回复

    另一件事可以尝试:

    .//span[span]/text()
    
  • 逗你玩~
    逗你玩~ 回复

    You were on track with your following-sibling:: attempt, but change the XPath,

    .//span[contains(@class, 'price--default')]/following-sibling::text()
    

    .//span[contains(@class, 'price--label')]/following-sibling::text()
    

    because it's the inner span after which you wish to select the following sibling text node.

    Note: If you know there's a single class attribute value, use = rather than the string containment predicate:

    .//span[@class='price--label']/following-sibling::text()
    

    If there could be multiple class attribute values, then use the space-padded idiom described here: XPath to match @class value and element value?