Q:

Remove items in string paragraph if they belong to a list of strings?

 import urllib2,sys
 from bs4 import BeautifulSoup,NavigableString

 obama_4427_url = 'http://www.millercenter.org/president/obama/speeches/speech-4427'
 obama_4427_html = urllib2.urlopen(obama_4427_url).read()

 obama_4427_soup = BeautifulSoup(obama_4427_html)

 # find the speech itself within the HTML

 obama_4427_div = obama_4427_soup.find('div',{'id': 'transcript'},{'class': 'displaytext'})

 # convert soup to string for easier processing

 obama_4427_str = str(obama_4427_div)

 # list of characters to be removed from obama_4427_str

 remove_char = ['<br/>','</p>','</div>','<div class="indent" id="transcript">','<h2>','</h2>','<p>']
 remove_char

 for char in obama_4427_str:
 if char in obama_4427_str:
     obama_4427_replace = obama_4427_str.replace(remove_char,'')

 obama_4427_replace = obama_4427_str.replace(remove_char,'')

 print(obama_4427_replace)

Using BeautifulSoup, I scraped one of Obama's speeches off of the above website. Now, I need to replace some residual HTML in an efficient manner. I've stored a list of elements I'd like to eliminate in remove_char. I'm trying to write a simple for statement, but am getting the error: TypeError: expected a character object buffer. It's a beginner question, I know, but how can I get around this?

A:

Since you are using BeautifulSoup already , you can directly use obama_4427_div.text instead of str(obama_4427_div) to get the correctly formatted text. Then the text you get would not contain any residual html elements, etc.
Example -
>>> obama_4427_div = obama_4427_soup.find('div',{'id': 'transcript'},{'class': 'displaytext'})
>>> obama_4427_str = obama_4427_div.text
>>> print(obama_4427_str)

Transcript
To Chairman Dean and my great friend Dick Durbin; and to all my fellow citizens of this great nation;

With profound gratitude and great humility, I accept your nomination for the presidency of the United States.

Let me express my thanks to the historic slate of candidates who accompanied me on this ...
...
...
...
Thank you, God Bless you, and God Bless the United States of America.

For completeness, for removing elements from a string, I would create a list of elements to remove (like the remove_char list you have created) and then we can do str.replace() on the string for each element in the list. Example -
obama_4427_str = str(obama_4427_div)
remove_char = ['<br/>','</p>','</div>','<div class="indent" id="transcript">','<h2>','</h2>','<p>']
for char in remove_char:
    obama_4427_str = obama_4427_str.replace(char,'')

