如何修复这个AttributeError?
我昨天安装了一个stripe的包,现在我的应用程序无法运行。我在试着找出问题出在哪里。是不是和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 个回答
1
你需要在父类 HTMLParser 中调用 init 方法。
你也可以通过以下方式来实现:
class MLStripper(HTMLParser):
def __init__(self):
super(MLStripper, self).__init__()
set()
self.fed = []
1
这个错误也会出现在你重写了HTMLParser类中的reset方法时。
在我的情况下,我为了实现其他功能添加了一个叫reset的方法,结果发现虽然Python并没有告诉我这样做有问题(也没有任何提示说我在重写什么),但这却导致了HTMLParser类出现了问题。
33
你发的代码有一两个问题(主要是关于如何正确初始化 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)