如何修复此AttributeError?

Zey*_*nel 14 html python google-app-engine html-parsing attributeerror

我昨天安装了一个条带包,现在我的应用程序没有运行.我试图了解问题所在.有什么事情做PyShellHTLParser或别的东西.我发布了GAE标签,希望日志中的跟踪可以提供有关问题的线索:

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__
    handler.post(*groups)
  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
    s.feed(html)
  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'
Run Code Online (Sandbox Code Playgroud)

这是MLStripper:

from HTMLParser import HTMLParser

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

def strip_tags(html):
    s = MLStripper()
    s.feed(html)
    return s.get_data()
Run Code Online (Sandbox Code Playgroud)

MLStripper工作正常,直到昨天.

这些是我的其他问题:

/sf/ask/570649901/

/sf/ask/570731031/

ekh*_*oro 27

您发布的代码有一两个问题(主要与HTMLParser正确初始化有关).

尝试运行此脚本的修订版本:

from HTMLParser import HTMLParser

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

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

    def handle_data(self, d):
        self._lines.append(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)
Run Code Online (Sandbox Code Playgroud)

  • 在派生类中定义__init__时,请记住显式调用基类的__init__.否则,base'__init__会被派生的__init__覆盖,这会导致原始帖子中出现未定义的属性问题. (2认同)