如何从其HTML代码打印表中存在的空数据? [英] How to print the empty data present in a table from its HTML code?
问题描述
我正在使用python中存在的HTMLParser模块通过通过HTMLParser解析HTML页面来在表中打印数据.我无法在表格中打印空白字段.
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>""")
结果-> 105
我能够在第一个标签<p>105</p>
之间打印数据.我想打印第二个标签<p></p>
之间存在的空数据.我该怎么办?
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;">"""
我想将数据打印为空字符串(").有帮助吗?..
推荐答案
好吧,如果您确实需要它,请尝试使用handle_endtag
:
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>""")
这样,每次标签结束时,您都将打印其中的数据.但是,这会将<p><p></p></p>
视为两次空数据"-在每个标签结束之前.如果这接近(但不完全是)所需,请尝试花一些时间同时使用handle_starttag
,这样您的代码就可以按照您希望的方式运行.
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.
这篇关于如何从其HTML代码打印表中存在的空数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!