tags:

views:

248

answers:

1

I am looking for a relatively simple HTML to text converter which displays links and works on strings.

So far I have tried

  • lynx but performance is too bad,
  • html2text which gives weird and verbose markdown output and is under GPLv3 which is too restrictive for my (BSD-licensed) project,
  • http://effbot.org/librarybook/formatter-example-3.py using htmllib.HTMLParser with formatter.AbstractFormatter and a custom writer, however htmllib.HTMLParser is drpeceated and has been removed from Python 3.

So is there any simple, performant, Python 3-compatible HTML to text converter under a permissive license such as MIT/BSD/Apache and the like?

Edit: I dont just need something to strip HTML-Tags but also to preserve the basic structure of the HTML, that is output that somewhat resembles that of Lynx.

A: 

Pyparsing's examples include an html stripper and a URL extractor.

Unlike BeautifulSoup and most HTML parsers, Pyparsing does not try to parse the entire HTML document, and return a hierarchical object. Pyparsing is closer to the typical regex scanner/filter engine - but pyparsing's HTML tag expressions are tolerant of many of the tricky variabilities that make regex processing of HTML a nightmare:

  • unpredictable upper/lower case
  • unpredictable whitespace before and after <, >, and =
  • unexpected attributes (like '')
  • attribute values in single quotes, or unquoted values
  • attributes in varying order
Paul McGuire
Thanks, I tried it. Unfortunately it is very slow, even slower than using lynx, and it only removes HTML tags. I am looking for something giving output similar to lynx, preserving basic structure, like lists or blockquotes.