Balakrishnan
Balakrishnan

Reputation: 2441

HTML to readable text

I'm writing a program to run on Google App Engine. Which simply get an URL and return the text by removing markups, scripts and any other non-readable things from its HTML source(similar to nltk.clear_html).

HtmlTool by Eike.

import urllib

class HtmlTool(object):
    import HTMLParser
    import re
    """
    Algorithms to process HTML.
    """
    #Regular expressions to recognize different parts of HTML. 
    #Internal style sheets or JavaScript 
    script_sheet = re.compile(r"<(script|style).*?>.*?(</\1>)", 
                              re.IGNORECASE | re.DOTALL)
    #HTML comments - can contain ">"
    comment = re.compile(r"<!--(.*?)-->", re.DOTALL) 
    #HTML tags: <any-text>
    tag = re.compile(r"<.*?>", re.DOTALL)
    #Consecutive whitespace characters
    nwhites = re.compile(r"[\s]+")
    #<p>, <div>, <br> tags and associated closing tags
    p_div = re.compile(r"</?(p|div|br).*?>", 
                       re.IGNORECASE | re.DOTALL)
    #Consecutive whitespace, but no newlines
    nspace = re.compile("[^\S\n]+", re.UNICODE)
    #At least two consecutive newlines
    n2ret = re.compile("\n\n+")
    #A return followed by a space
    retspace = re.compile("(\n )")

    #For converting HTML entities to unicode
    html_parser = HTMLParser.HTMLParser()

    @staticmethod
    def to_nice_text(html):
        """Remove all HTML tags, but produce a nicely formatted text."""
        if html is None:
            return u""
        text = html
        text = HtmlTool.script_sheet.sub(" ", text)
        text = HtmlTool.comment.sub(" ", text)
        text = HtmlTool.nwhites.sub(" ", text)
        text = HtmlTool.p_div.sub("\n", text) #convert <p>, <div>, <br> to "\n"
        text = HtmlTool.tag.sub(" ", text)     #remove all tags
        text = HtmlTool.html_parser.unescape(text)
        #Get whitespace right
        text = HtmlTool.nspace.sub(" ", text)
        text = HtmlTool.retspace.sub("\n", text)
        text = HtmlTool.n2ret.sub("\n\n", text)
        text = text.strip()
        return text

It works for 'http://google.com'

text = HtmlTool.to_nice_text(urllib.urlopen('http://google.com').read())

But, It throws an error for 'http://yahoo.com'

text = HtmlTool.to_nice_text(urllib.urlopen('http://yahoo.com').read())

Error:

Traceback (most recent call last):
  File "C:\Users\BK\Desktop\Working Folder\AppEngine\crawlnsearch\-test.py", line 51, in <module>
    text = HtmlTool.to_nice_text(urllib.urlopen('http://yahoo.com').read())
  File "C:\Users\BK\Desktop\Working Folder\AppEngine\crawlnsearch\-test.py", line 43, in to_nice_text
    text = HtmlTool.html_parser.unescape(text)
  File "C:\Python27\lib\HTMLParser.py", line 472, in unescape
    return re.sub(r"&(#?[xX]?(?:[0-9a-fA-F]+|\w{1,8}));", replaceEntities, s)
  File "C:\Python27\lib\re.py", line 151, in sub
    return _compile(pattern, flags).sub(repl, string, count)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 1531: ordinal not in range(128)

So, can any one explain whats wrong with this code and post a fix for this or please tell me how to use nltk.clean_html.

Upvotes: 0

Views: 892

Answers (1)

H.D.
H.D.

Reputation: 4454

That's because there's a mix between unicode and bytestrings.

If you use in the module with HtmlTool

from __future__ import unicode_literals

To ensure every " "-like blocks are unicode, and

text = HtmlTool.to_nice_text(urllib.urlopen(url).read().decode("utf-8"))

To send a UTF-8 string to your method, that solves your problem.

Please read this for more information about Unicode: http://www.joelonsoftware.com/articles/Unicode.html

Upvotes: 1

Related Questions