python - 使用 XPATH提取两个标签之间的所有文本(粗体)
问题描述
解决方案
最快的方法可能是string(//p)
使用特定的文本操作命令获取所有内容并进行拆分。
使用 XPath,您可以尝试:
获取所有标题(返回 5 个元素):
//b/text()
使用这些 XPath(返回 5*1 元素)获取相应的描述(包括斜体标记):
normalize-space(substring-before(substring-after(string(//p),//b[.="Introduction."]),//b[.="Aim."]))
normalize-space(substring-before(substring-after(string(//p),//b[.="Aim."]),//b[.="Methods."]))
normalize-space(substring-before(substring-after(string(//p),//b[.="Methods."]),//b[.="Results."]))
normalize-space(substring-before(substring-after(string(//p),//b[.="Results."]),//b[.="Conclusion."]))
normalize-space(substring-after(string(//p),//b[.="Conclusion."]))
如果您不知道标签之间的文本,您可以使用按位置索引 (//b[1],//b[2],...)。使用 count(//b) 知道最大值。
编辑:替代 XPaths:
normalize-space(//text()[preceding::b="Introduction." and following::b="Aim."])
normalize-space(//text()[preceding::b="Aim." and following::b="Methods."])
normalize-space(//text()[preceding::b="Methods." and following::b="Results."])
normalize-space(//text()[preceding::b="Results." and following::b="Conclusion."])
normalize-space(//text()[preceding::b="Conclusion."])
推荐阅读
- javascript - JavaScript:递归异步/基于承诺的函数
- regex - 将文本转换为数字文本
- codenameone - 自定义搜索过滤器和日历代号一
- syntax - Clojure.core 来源:为什么 ~@ (取消引用拼接运算符)内部带有带引号的双列表,而不是 ~ (取消引用运算符)
- asp.net-core - 我们如何禁用添加到 ASP.NET Core 日志的 ANSI/VT100 颜色代码
- openid - OIDC (OpenID Connect) 是否完全基于 REST?
- cluster-analysis - 对 RDF 文件使用 K-means
- c# - Parallel.Foreach 与 OledbDataReader 调用 web api 导致重复行
- reactjs - 如何在 React JS 中的“react-bootstrap-wizard”的下一个按钮上添加点击事件
- java - 用于 Junit 测试的具有零和两个结果的 ArrayList