[英]How to fix this AttributeError?
我昨天安裝了一個條紋包,現在我的應用程序沒有運行。 我試圖了解問題出在哪里。 是否與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
您發布的代碼存在一兩個問題(主要與正確初始化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)
如果您覆蓋 HTMLParser 類中的 reset 方法,也會出現此錯誤。
在我的例子中,我為其他一些功能添加了一個名為 reset 的方法,並發現雖然 Python 沒有告訴你這樣做有問題(也沒有任何跡象表明我覆蓋了任何東西),但它破壞了 HTMLParser 類。
您需要在超類 HTMLParser 中調用init 。
你也可以使用
class MLStripper(HTMLParser):
def __init__(self):
super(MLStripper, self).__init__()
set()
self.fed = []
我有一個類似的問題,我的意思是原始數據的屬性錯誤。
我一開始使用了以下語法,在我看來是正確的
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
然后它起作用了。
這讓我很困惑,我認為我們正在做的兩種語法是一樣的,但看起來它們不是。
如果有人可以解釋我為什么這是≠
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.