【发布时间】:2023-03-08 03:25:01
【问题描述】:
我想要与 this jQuery question 等效的 BeautifulSoup。
我想在 BeautifulSoup 文本中找到一个特定的正则表达式匹配,然后用包装的版本替换该段文本。我可以用纯文本换行来做到这一点:
# replace all words ending in "ug" wrapped in quotes,
# with "ug" replaced with "ook"
>>> soup = BeautifulSoup("Snug as a bug in a rug")
>>> soup
<html><body><p>Snug as a bug in a rug</p></body></html>
>>> for text in soup.findAll(text=True):
... if re.search(r'ug\b',text):
... text.replaceWith(re.sub(r'(\w*)ug\b',r'"\1ook"',text))
...
u'Snug as a bug in a rug'
>>> soup
<html><body><p>"Snook" as a "book" in a "rook"</p></body></html>
但是如果我想要粗体而不是引号怎么办?例如期望的结果 =
<html><body><p><b>Snook</b> as a <b>book</b> in a <b>rook</b></p></body></html>
【问题讨论】:
标签: python html regex beautifulsoup