17

我昨天安装了一个条带包,现在我的应用程序没有运行。我试图了解问题出在哪里。是跟什么有关PyShell还是HTLParser别的什么。我也发布了带有 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'

这是 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()

MLStripper 工作正常,直到昨天。

这些是我的其他问题:

https://stackoverflow.com/questions/8152141/how-to-fix-this-attributeerror-with-htmlparser-py

https://stackoverflow.com/questions/8153300/how-to-fix-a-corrupted-pyshell-py

4

4 回答 4

32

您发布的代码存在一两个问题(主要与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)
于 2011-11-16T15:27:26.277 回答
1

如果您覆盖 HTMLParser 类中的 reset 方法,也会出现此错误。

在我的例子中,我为其他一些功能添加了一个名为 reset 的方法,并发现虽然 Python 没有告诉你这样做有问题(也没有任何迹象表明我正在覆盖任何东西),但它破坏了 HTMLParser 类。

于 2016-05-24T02:20:10.650 回答
1

您需要在超类 HTMLParser 中调用init

你也可以使用

class MLStripper(HTMLParser):
    def __init__(self):
        super(MLStripper, self).__init__()
        set()
        self.fed = []
于 2019-07-12T11:08:33.370 回答
0

我有一个类似的问题,我的意思是原始数据的属性错误。

一开始我使用了下面的语法,这对我来说是正确的

class pdbResaHTMLParser(HTMLParser):
    def __init__(self,booking: Booking):
        super(HTMLParser, self).__init__()
        theBooking = booking

结果是

AttributeError: 'pdbResaHTMLParser' object has no attribute 'rawdata'

稍后调用 feed 方法时。

阅读那篇文章,我没有真正的希望将代码更改为

class pdbResaHTMLParser(HTMLParser):
    def __init__(self,booking: Booking):
        HTMLParser.__init__(self)
        #super(HTMLParser, self).__init__()
        theBooking = booking

然后它就起作用了。

这让我感到困惑,我认为这两种语法我们正在做同样的事情,但看起来它们不是。

如果有人能解释一下为什么这是≠

于 2020-05-08T09:51:45.793 回答