问题
I am using the HTMLParser module present in python to print the data in a table by parsing the HTML page through the HTMLParser. I am unable to print the empty field in the table.
Here is the code I'm using:
class MyParser(HTMLParser):
def __init__(self, data ):
HTMLParser.__init__(self)
self.feed(data)
def handle_data(self, data):
print "result -->", data
m = MyParser("""<p>105</p><p></p>""")
result --> 105
I am able to print the data between the first tag <p>105</p>
. I want to print the empty data present between the second tag <p></p>
. How do I do it?
HTMLPAGE = """<p>105</p></td><td style="width:50px; word-wrap: break-word;"><p style="width: 8em; padding-left: 0px; padding-right: 0px; margin: 0pt;"></p></td><td style="width:50px; word-wrap: break-word;">"""
I want print the data as the empty string(""). Any help?..
回答1:
Well, if you really need this, try using handle_endtag
:
class MyParser(HTMLParser):
def __init__(self, data ):
HTMLParser.__init__(self)
self.data = ""
self.feed(data)
def handle_data(self, data):
self.data = data
def handle_endtag(self, tag, attrs):
print "result -->", self.data
self.data = ""
m = MyParser("""<p>105</p><p></p>""")
This way every time tag ends, you will print the data that was inside. This will, however, treat <p><p></p></p>
as two times "empty data" - before every tag ending. If this is close to (but not exactly) what you need, try spending some time on using also handle_starttag
, so your code can behave the way you want it to.
来源:https://stackoverflow.com/questions/7007858/how-to-print-the-empty-data-present-in-a-table-from-its-html-code