【问题标题】:Python Scrapy selector for text elements between tags用于标签之间文本元素的 Python Scrapy 选择器
【发布时间】:2021-03-24 00:44:51
【问题描述】:

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

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

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 文本值中获取值?

【问题讨论】:

标签: css python-3.x scrapy


【解决方案1】:

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

cbd = response.xpath('//strong[.="CBD:"]/following-sibling::text()[1]').get()
thc = response.xpath('//strong[.="THC:"]/following-sibling::text()[1]').get()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-15
    • 2014-09-28
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    • 2019-04-20
    相关资源
    最近更新 更多