Im relative new python im trying the html.parser as fallow:
from html.parser import HTMLParser
import urllib.request
class TestParser(HTMLParser):
def handle_starttag(self, tag, attrs):
print("Start Tag: ", tag, attrs)
def handle_endtag(self, tag):
print("End Tag: ", tag)
def handle_data(self, data):
print("Data: ", data)
def handle_startendtag(self,tag,attrs):
print("StarEnd Tag: ", tag, attrs)
class DanParser(HTMLParser):
def __init__(self):
super(DanParser, self).__init__(strict = False)
self.in_select = False
def handle_starttag(self, tag, attrs):
print("Start Tag: ", tag, attrs)
if tag == "select":
self.in_select = True
print("Start Tag: ", tag, attrs)
def handle_endtag(self, tag):
print("EndTag: ", tag)
if tag == "select" and self.in_select:
self.in_select = False
print("EndTag: ", tag)
def handle_data(self, data):
print("Data: ", data)
if self.in_select:
print("Data: ", data)
def handle_startendtag(self,tag,attrs):
print("StarEnd Tag: ", tag, attrs)
When i do in the interpreter
t = new DanParser()
t.feed("<select>test</select>")
im getting:
Data: <select>
Data: test
EndTag: select
the method handle_starttag is not been called, but when i do it using the TestParser its behaving correctly. Can anyone tell me what im doing wrong!!! thanks
string=Falsemean? According to the docs HTMLParser doesn't take arguments. Alsot = new DanParser()is a syntax error and your indentation is incorrect. Fixing these and your example works for me.HTMLParser()hasstrictargument since Python3.2