Python 标签之间文本元素的 Scrapy 选择器
Python Scrapy selector for text elements between tags
我需要在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%
如何从强文本值和第二文本值中获取值?
这里不需要类。我强烈建议切换到 XPath:
cbd = response.xpath('//strong[.="CBD:"]/following-sibling::text()[1]').get()
thc = response.xpath('//strong[.="THC:"]/following-sibling::text()[1]').get()
我需要在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%
如何从强文本值和第二文本值中获取值?
这里不需要类。我强烈建议切换到 XPath:
cbd = response.xpath('//strong[.="CBD:"]/following-sibling::text()[1]').get()
thc = response.xpath('//strong[.="THC:"]/following-sibling::text()[1]').get()