我有一个脚本来替换"ahref"标签中的单词.但是,我想完全删除一个href,这样你就没有链接了.
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup('<p>Hello <a href="http://google.com">Google</a></p>')
for a in soup.findAll('a'):
a['href'] = a['href'].replace("google", "mysite")
result = str(soup)
Run Code Online (Sandbox Code Playgroud)
你也可以找到放在href中的所有单词,并在它们之前和之后放置一个"".我不知道该怎么做.我想这是在更换之前完成的.
假设我有
text = """ <a href = 'http://www.crummy.com/software'>Hello There</a>"""
Run Code Online (Sandbox Code Playgroud)
我想用一个空格(“”)替换 a hrefs 和 /a 。在它的位置。顺便说一句,它是一个 BeautifulSoup.BeautifulSoup 类。所以正常的 .replace 是行不通的。
我希望文字只是
""" Hello There """
Run Code Online (Sandbox Code Playgroud)
注意“Hello There”前后的空格。