In the below code, the compiler is giving me the error “Local variable shouldStoreData” referenced before assignment. But I am assigning it first.
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
What I am trying to do here is that I want to store the data only when the tag is title or any tag within body.
The first
shouldStoreDatais a class attribute. The ones insidehandle_starttagandhandle_dataare local variables. Inhandle_data, you test the variable before you assign it. Code inside the method doesn’t know that the class attribute exists unless you explictly access it as a class/instance attribute (e.g.,self.shouldStoreDataorMyHTMLParser.shouldStoreData).If you want
shouldStoreDatato be an instance attribute, then you need to write an__init__method and create it as an instance attribute:and then later refer to it as
self.shouldStoreDatain your other methods.You should read the Python tutorial to get a grasp of the basics of classes, attributes, and methods in Python.