如何在 beautifulsoup 中处理正则表达式

问题描述 投票:0回答:1
check_regex = re.compile("HO\s?#",re.IGNORECASE)

check_ho_number3 = soup.select_one('td:-soup-contains("HO #")+ td')
print(check_ho_number3)

有没有任何解决方案来处理正则表达式,其中汤包含“HO #”可能有空格并忽略大小写。我已经编写了正则表达式如何包含在汤选择中

python regex beautifulsoup
1个回答
0
投票

CSS SELECTORS
仅支持 CSS 语法,但您可以使用
string
属性按标签内容进行搜索:

from bs4 import BeautifulSoup
import re

soup = BeautifulSoup('<table><tr><td>HO #</td><td>I am the next sibling</td></tr><tr><td>HO #</td></tr><tr><td>ho#</td><td>I am the next sibling</td></tr></table>')

for e in soup.find_all(string=re.compile(r"HO\s?#",re.IGNORECASE)):
    print(e)
    print(e.parent.findNextSibling('td'))

HO #
<td>I am the next sibling</td>
HO #
None
ho#
<td>I am the next sibling</td>
© www.soinside.com 2019 - 2024. All rights reserved.