只返回段落的第一部分,直到遇到子标记为止?

2024-04-25 06:01:18 发布

您现在位置:Python中文网/ 问答频道 /正文

这是用刮痧。你知道吗

我遇到了以下类型的标记:

<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")

测试结果:

Noting the presence of a footnote

预期测试:

Noting the presence of a footnote is one common way for superscripts to be used.

问题:

忽略子标签,如何获取段落的全文?你知道吗


Tags: ofthetotextforisbecommon
1条回答
网友
1楼 · 发布于 2024-04-25 06:01:18

我不知道scrapy是否有适当的选择器忽略嵌套的<sub>。我建议您使用re模块来忽略这个孩子。顺便说一下,从长远来看,这不是一个解决办法。不应该使用regex解析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)

相关问题 更多 >