Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a tag with space Beautiful Soup

Suppose i have

text = """ <a href = 'http://www.crummy.com/software'>Hello There</a>"""

I want to replace the a hrefs and /a with just put a space (" "). In its place. BTW its a BeautifulSoup.BeautifulSoup Class. So normal .replace wouldn't work.

I want text to be just

""" Hello There """

Notice the space after and before "Hello There".

like image 801
user2784753 Avatar asked Sep 30 '13 08:09

user2784753


People also ask

How do you replace a tag in beautiful soup?

To replace a tag in Beautful Soup, find the element then call its replace_with method passing in either a string or tag.

How do you replace text in HTML using Python?

If the text and the string to replace is simple then use str. replace().


1 Answers

You can use replaceWith() (or replace_with()):

from bs4 import BeautifulSoup

soup = BeautifulSoup("""
<html>
 <body>
  <a href = 'http://www.crummy.com/software'>Hello There</a>
 </body>
</html>
""")

for a in soup.findAll('a'):
    a.replaceWith(" %s " % a.string)

print soup

prints:

<html><body>
 Hello There 
</body></html>
like image 164
alecxe Avatar answered Nov 06 '22 21:11

alecxe