tags:

views:

80

answers:

5

I have a string that contains html markup like links, bold text, etc.

I want to strip all the tags so I just have the raw text.

What's the best way to do this? regex?

+3  A: 

If you are going to use regex:

import re
def striphtml(data):
    p = re.compile(r'<.*?>')
    return p.sub('', data)

>>> striphtml('<a href="foo.com" class="bar">I Want This <b>text!</b></a>')
'I Want This text!'
Zonda333
This will only work reliably on well-formed HTML (ie, no unescaped `<` or `>` outside of actual tags, no malformed tags like `<b class="forgot-to-close"`, etc.). That being said, this is the first approach I'd use, depending on the source data.
Will McCutchen
A: 

Depending on whether the text will contain '>' or '<' I would either just make a function to remove anything between those, or use a parsing lib

def cleanStrings(self, inStr):
  a = inStr.find('<')
  b = inStr.find('>')
  if a < 0 and b < 0:
    return inStr
  return cleanString(inStr[a:b-a])
snurre
+4  A: 

AFAIK using regex is a bad idea for parsing HTML, you would be better off using a HTML/XML parser like beautiful soup.

volting
+1 for Beautiful Soup
derekerdmann
I am using beautifulsoup, but I want to be able to strip html tags manually also. thanks!
Blankman
@Blankman it would of been a good idea to mention that in your question
volting
+1  A: 

Use SGMLParser. regex works in simple case. But there are a lot of intricacy with HTML you rather not have to deal with.

>>> from sgmllib import SGMLParser
>>>
>>> class TextExtracter(SGMLParser):
...     def __init__(self):
...         self.text = []
...         SGMLParser.__init__(self)
...     def handle_data(self, data):
...         self.text.append(data)
...     def getvalue(self):
...         return ''.join(ex.text)
...
>>> ex = TextExtracter()
>>> ex.feed('<html>hello &gt; world</html>')
>>> ex.getvalue()
'hello > world'
Wai Yip Tung
A: 

Use lxml.html. It's much faster than BeautifulSoup and raw text is a single command.

>>> import lxml.html
>>> page = lxml.html.document_fromstring('<!DOCTYPE html>...</html>')
>>> page.cssselect('body')[0].text_content()
'...'
Tim McNamara