【发布时间】:2019-07-01 12:55:24
【问题描述】:
我想用 BeautifulSoup 识别大文本文档中的分割点。因此,我制定了一个正则表达式来查找特定字符串出现的Tag。问题是,如果我正在搜索的字符串中还有其他格式/子节点,它就不起作用。
t1 = BeautifulSoup("<p class=\"p p8\"><strong>Question-And-Answer</strong></p>")
t2 = BeautifulSoup("<p class=\"p p8\"><strong>Question</strong>-<strong>And</strong>-<strong>Answer</strong></p>")
t1.find(text = re.compile("Question[s]*-And-Answer[s]*", re.IGNORECASE))
>>> 'Question-And-Answer'
t2.find(text = re.compile("Question[s]*-And-Answer[s]*", re.IGNORECASE))
>>> None
输出应该是p Tag 对象。
【问题讨论】:
-
不确定这是否有帮助,但如果你这样做
re.match("Question[s]*-And-Answer[s]*", t2.text).group(),它会输出你想要的结果。但就像我说的,不确定这是否有助于您继续前进。 -
我下面的回答能解决问题吗?请告知仍然缺少或需要澄清的内容。
标签: python regex beautifulsoup