擅长:python、mysql、java
<p>它似乎对我的代码不起作用,所以我像一种全局变量一样在外部定义了<code>tag_stack = []</code>。</p>
<pre><code>from html.parser import HTMLParser
tag_stack = []
class MONanalyseur(HTMLParser):
def handle_starttag(self, tag, attrs):
tag_stack.append(tag.lower())
def handle_endtag(self, tag):
tag_stack.pop()
def handle_data(self, data):
if tag_stack[-1] == 'head':
print(data)
parser=MONanalyseur()
parser.feed()
</code></pre>