2
votes

I'd like to extract the text from an HTML file using Python. I want essentially the same output I would get if I copied the text from a browser and pasted it into notepad.

I'd like something more robust than using regular expressions that may fail on poorly formed HTML. I've seen many people recommend Beautiful Soup, but I've had a few problems using it. For one, it picked up unwanted text, such as JavaScript source. Also, it did not interpret HTML entities. For example, I would expect ' in HTML source to be converted to an apostrophe in text, just as if I'd pasted the browser content into notepad.

Update: html2text looks promising. It handles HTML entities correctly and ignores JavaScript. However, it does not exactly produce plain text; it produces markdown that would then have to be turned into plain text. It comes with no examples or documentation, but the code looks clean.

2
If only life were so easy... do you know how HTML works? Have you opened up your HTML file in e.g. Notepad?Katriel
question updated please have a look.........Cold-Blooded
there is no magic tool that will remove everything. all web pages are going to be loaded with stuff. better to grab it all, then replace apostrophes and remove javascript.JiminyCricket

2 Answers

6
votes

you would need to use urllib2 python library to get the html from the website and then parse through the html to grab the text that you want.

Use BeautifulSoup to parse through the html

import BeautifulSoup
resp = urllib2.urlopen("http://stackoverflow.com")
rawhtml = resp.read()
#parse through html to get text
soup=BeautifulSoup(rawhtml)
1
votes

I don't "copy-paste from browser" is a well-defined operation. For instance, what would happen if the entire page were covered with a transparent floating div? What if it had tables? What about dynamic content?

BeautifulSoup is a powerful parser; you just need to know how to use it (it is easy, for instance, to remove the script tags from the page). Fortunately, it has a lot of documentation.

You can use xml.sax.utils.unescape to unescape HTML entities.