【问题标题】:How to fix this AttributeError?如何修复此 AttributeError?
【发布时间】:2011-12-30 12:41:45
【问题描述】:

我昨天安装了一个条带包,现在我的应用程序没有运行。我试图了解问题出在哪里。是否与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'

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

【问题讨论】:

    标签: python html google-app-engine html-parsing attributeerror


    【解决方案1】:

    您发布的代码存在一两个问题(主要与正确初始化 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)
    

    【讨论】:

    • 非常感谢您的回答。它工作得很好。您介意在代码中添加一些 cmets 以便我理解吗?还有为什么你认为这已经工作了几个月,然后突然停止工作了。再次感谢。
    • @Zeynel。我添加了一些 cmets 来显示我对您的原始脚本所做的主要更改。至于为什么您以前的脚本停止工作:如果不知道您的系统最近发生了什么变化,这很难说。但无论如何,我认为修改后的脚本更普遍是正确的。
    • 任何将来可能访问这里的 stackoverflow 人员:如果您覆盖 __init__ 方法,这是特别好的建议。
    • 在派生类中定义init时,记得显式调用基类的init。否则,base 的 init 会被派生的 init 覆盖,导致原帖出现未定义属性问题。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      您需要在超类 HTMLParser 中调用 init

      你也可以使用

      class MLStripper(HTMLParser):
          def __init__(self):
              super(MLStripper, self).__init__()
              set()
              self.fed = []
      

      【讨论】:

        【解决方案4】:

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

        我一开始使用的语法对我来说是正确的

        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
        

        然后它就起作用了。

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-07-13
          • 2019-12-10
          • 1970-01-01
          • 2020-03-11
          • 1970-01-01
          • 2012-10-17
          • 1970-01-01
          相关资源
          最近更新 更多