Getting rid of certain HTML tags

This code simply returns a small section of HTML code and then gets rid of all tags except for break tags.

It seems inefficient because you cannot search and replace with a beautiful soup object as you can with a Python string, so I was forced to switch it back and forth from a beautiful soup object to a string several times so I could use string functions and beautiful soup functions. It seems that there must be a simpler way to do this without switching back and forth between soup objects and strings.

def whiteScrape(webaddress):

soup = BeautifulSoup(ourUrl)
soup = soup.find("div", { "class" : "addy" })
#below code will delete tags except /br
soup = str(soup)
soup = soup.replace('<br/>' , '^')
soup = BeautifulSoup(soup)
soup = (soup.get_text())
soup = str(soup)
soup=soup.replace('^' , '<br/>')

return soup


The original HTML is something like:

<div class = "addy"> <p> blah blah <br/> blahblah <br/> blah </p></div>


The output should be:

blah blah <br/> blahblah <br/> blah


By PEP 8, whiteScrape() should be renamed to white_scrape().

Variable names should be purposeful. Therefore, it's a bad idea to continually redefine soup. If someone were to ask you to explain what the variable soup contained, you would have a hard time explaining.

It seems like what you want to do is to stringify the children of div(s) in question. One way to do that would be:

def white_scrape(url):

Note that this is not exactly equivalent to your original code. For example, if an address contains an element, such as 123 1<sup>st</sup> St., this solution would preserve the <sup> tag and its contents, whereas your original code would discard the tag but keep its contents and strip the tag.