在下面的代码中,编译器给了我赋值前引用的错误“Local variable shouldStoreData”。但我先分配它。
class MyHTMLParser(HTMLParser):
shouldStoreData = False
textPartDoc = ""
def handle_starttag(self, tag, attrs):
print "Encountered a start tag:", tag
if(tag == "title" or tag == "body"):
shouldStoreData = True
def handle_endtag(self, tag):
print "Encountered an end tag :", tag
def handle_data(self, data):
#print "Data is",data
#print "valus of storeData is:",shouldStoreData
if(shouldStoreData == True):
textPartDoc = textPartDoc + " " + data
shouldStoreData = False
我在这里要做的是,我只想在标签是标题或正文中的任何标签时存储数据。