python - 只返回段落的第一部分,直到遇到子标签?
问题描述
这是使用 Scrapy。
我遇到以下类型的标记:
<p>Noting the presence of a footnote<sup>1</sup> is one common way for superscripts to be used.</p>
测试页面:
https ://html.com/tags/sup/
测试查询:
response.css('div.render p::text')[0].extract()
测试项目加载器:
loader.add_css("text", "div.render p::text")
试验结果:
注意到脚注的存在
预期测试:
注意脚注的存在是使用上标的一种常见方式。
问题:
如何获取段落的全文,忽略子标签?
解决方案
我不知道scrapy是否有适当的选择器忽略嵌套的<sub>
. 我建议您使用re
模块来忽略孩子。顺便说一句,从长远来看,这不是一个解决方案。您不应该使用正则表达式解析 HTML。有关更多信息,请查看此线程RegEx match open tags except XHTML self-contained tags
尝试这个 :
import re
def parse(self,response):
extracted_p_tag=response.css('div.render p').get()
ignored_sup=re.sub('<sup>(.*)</sup>','',extracted_p_tag)
推荐阅读
- r - 通过最后一次出现的字符串用 tidyr 分隔列
- python - 有没有办法通过 Python (pywin32) 在 Excel 上访问切片器
- api - 如何根据用户请求路由到特定的 docker 容器
- bash - 如何从 grepped 字符串中获取数据?
- r - 在 ggplot2 中随时间绘制多条线;希望能更好的区分线条
- c - clearerr 是做什么用的?
- r - 从 tmplot 切换到树图的简单 R 代码更新(无经验)
- z3 - 如何在 Z3py 中改进基于二分搜索的优化
- javascript - 如何使用 JavaScript 切换 SVG 圆圈
- excel - 在输入框中输入日期并在VBA上找到上个月的日期