我试图在xpath的范围内获得价格。由于它包含一个跨度,因此我得到的唯一值是\ n:
HTML:
<span class="price--default is--nowrap is--discount">
<span class="price--label">Unser Preis:</span> 19,95 €
</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标签获取价格?
另一件事可以尝试:
You were on track with your
following-sibling::
attempt, but change the XPath,至
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:If there could be multiple
class
attribute values, then use the space-padded idiom described here: XPath to match @class value and element value?