Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search and Replace in HTML with BeautifulSoup

I want to use BeautfulSoup to search and replace <\a> with <\a><br>. I know how to open with urllib2 and then parse to extract all the <a> tags. What I want to do is search and replace the closing tag with the closing tag plus the break. Any help, much appreciated.

EDIT

I would assume it would be something similar to:

soup.findAll('a').

In the documentation, there is a:

find(text="ahh").replaceWith('Hooray')

So I would assume it would be along the lines of:

soup.findAll(tag = '</a>').replaceWith(tag = '</a><br>')

But that doesn't work and the python help() doesn't give much

like image 757
Kevin Avatar asked Jan 15 '10 17:01

Kevin


2 Answers

This will insert a <br> tag after the end of each <a>...</a> element:

from BeautifulSoup import BeautifulSoup, Tag

# ....

soup = BeautifulSoup(data)
for a in soup.findAll('a'):
    a.parent.insert(a.parent.index(a)+1, Tag(soup, 'br'))

You can't use soup.findAll(tag = '</a>') because BeautifulSoup doesn't operate on the end tags separately - they are considered part of the same element.


If you wanted to put the <a> elements inside a <p> element as you ask in a comment, you can use this:

for a in soup.findAll('a'):
    p = Tag(soup, 'p') #create a P element
    a.replaceWith(p)   #Put it where the A element is
    p.insert(0, a)     #put the A element inside the P (between <p> and </p>)

Again, you don't create the <p> and </p> separately because they are part of the same thing.

like image 96
interjay Avatar answered Sep 28 '22 07:09

interjay


suppose you have an element which you know contains the "br" markup tags, one way to remove & replace the "br" tags with a different string is like this:

originalSoup = BeautifulSoup("your_html_file.html")
replaceString = ", " # replace each <br/> tag with ", "
# Ex. <p>Hello<br/>World</p> to <p>Hello, World</p>
cleanSoup = BeautifulSoup(str(originalSoup).replace("<br/>", replaceString))
like image 33
Achintya Ashok Avatar answered Sep 28 '22 06:09

Achintya Ashok