Zeynel Zeynel - 2 years ago 173
Python Question

How to fix this AttributeError?

I installed a stripe package yesterday and now my app is not running. I am trying to understand where the problem is. Is it something to do with

or something else. I am posting with GAE tag as well hoping that the trace from logs may give a clue about the problem:

MLStripper instance has no attribute 'rawdata'
Traceback (most recent call last):
File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/_webapp25.py", line 703, in __call__
File "/base/data/home/apps/ting-1/1.354723388329082800/ting.py", line 2070, in post
pitch_no_tags = strip_tags(pitch_original)
File "/base/data/home/apps/ting-1/1.354723388329082800/ting.py", line 128, in strip_tags
File "/base/python_runtime/python_dist/lib/python2.5/HTMLParser.py", line 107, in feed
self.rawdata = self.rawdata + data
AttributeError: MLStripper instance has no attribute 'rawdata'

This is MLStripper:

from HTMLParser import HTMLParser

class MLStripper(HTMLParser):
def __init__(self):
self.fed = []
def handle_data(self, d):
def get_data(self):
return ''.join(self.fed)

def strip_tags(html):
s = MLStripper()
return s.get_data()

MLStripper was working fine until yesterday.

And these are my other questions:

How to fix this AttributeError with HTMLParser.py

How to fix a corrupted PyShell.py

Answer Source

There are one or two issues with the code you posted (mainly to do with initializing the HTMLParser properly).

Try running this amended version of your script:

from HTMLParser import HTMLParser

class MLStripper(HTMLParser):
    def __init__(self):
        # initialize the base class

    def read(self, data):
        # clear the current output before re-use
        self._lines = []
        # re-set the parser's state before re-use
        return ''.join(self._lines)

    def handle_data(self, d):

def strip_tags(html):
    s = MLStripper()
    return s.read(html)

html = """Python's <code>easy_install</code>
 makes installing new packages extremely convenient.
 However, as far as I can tell, it doesn't implement
 the other common features of a dependency manager -
 listing and removing installed packages."""

print strip_tags(html)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download