0

我需要在 Python Scrapy 中编写一个选择器。

我想获得 CBD 的 % 和 THC 的 %。

CSS

test = productResponse
    .css('.woocommerce-product-details__short-description > p')[1]
    .get()

当我尝试做这样的事情时,我得到了结果:

<p>
    <strong>CBD:</strong> 7.5%<br>
    <strong>THC:</strong><0.2%<br>
    <strong>Waga:</strong> 1 gram
</p>

但是当我添加时,::text我只得到 CBD 的值:

test = productResponse
    .css('.woocommerce-product-details__short-description > p::TEXT')[1]
    .get()

结果:

7.5%

如何从 Strong 和 second text 值中获取价值?

4

1 回答 1

2

你不需要在这里上课。我强烈建议切换到 XPath:

cbd = response.xpath('//strong[.="CBD:"]/following-sibling::text()[1]').get()
thc = response.xpath('//strong[.="THC:"]/following-sibling::text()[1]').get()
于 2020-12-13T09:35:58.267 回答