python - Scrapy Xpath 不包含命令不起作用
问题描述
我在我的 xpath 中使用了 not contains,但它似乎不起作用,因为它仍然在我的 not contains 中获取标题为“我不喜欢公司的内容”的 h2 下的元素。
HTML:
<div itemprop="reviewBody" class="review-body"><h2 class="h3">Suggestions for improvement</h2><p></p><ul><li>Better managers the ones they have suck</li></ul><h2 class="h3">What I like about the company</h2><p>Great company thanks again for sure</p><h2 class="h3">What I dislike about the company</h2><p>The fact they didn't care about my health</p></div>
Xpath:
response.xpath("(//div[@class='review-body'])/h2[contains(.,'What I like about the company') and not(contains(.,'What I dislike about the company'))]/following-sibling::p/text()").getall
我需要代码来提取 h2 下标题为“我喜欢公司的地方”的“p”,而不是“我不喜欢公司的地方”下的“p”,谢谢
解决方案
如果我理解正确,您想获取包含特定文本p
之后的第一个文本。h2
要实现这一点,只需一步一步地进行:
- 得到想要的
h2
://h2[text()="What I like about the company"]
- 得到它的第一个兄弟,它是
p
:/following-sibling::p[1]
- 得到它的文字:
/text()
把它们放在一起,我们得到这个:
>>> sel.xpath('//h2[text()="What I like about the company"]/following-sibling::p[1]/text()').get()
'Great company thanks again for sure'
推荐阅读
- c# - 如何转换 json obj(字典
>) 到字典 > 使用 Newtonsoft.Json.Linq 的常规 obj - java - 使用 HTMLunit 登录重定向到 goolge 验证码
- css - 如何将整页背景设置为 mat-spinner
- php - 根据变量显示 X 输入
- input - Keras:如何设计神经网络的输入层和形状?
- c#-4.0 - 除了 .Net 核心,如何在 .net 框架中实现 IHttpClientFactory?
- dji-sdk - SDK 4.6 - 对 Spark 的 Waypoint Mission、Follow Me Mission 和 HotPoint Mission 支持
- java - 春季网络逻辑错误
- wechat - 分享到微信:WeixinJSBridge 未定义
- javascript - 一旦其他输入字段填充了至少 6 个字符,如何删除“禁用”属性