bsp python学习 () parser rom body import ...
- 使用Python解析html文件
- from html.parser import HTMLParser
- class MyHTMLParser(HTMLParser):
- def handle_starttag(self, tag, attrs):
- print(‘<%s>‘ % tag)
- def handle_endtag(self, tag):
- print(‘</%s>‘ % tag)
- def handle_startendtag(self, tag, attrs):
- print(‘<%s/>‘ % tag)
- def handle_data(self, data):
- print(data)
- def handle_comment(self, data):
- print(‘<!--‘, data, ‘-->‘)
- def handle_entityref(self, name):
- print(‘&%s;‘ % name)
- def handle_charref(self, name):
- print(‘&#%s;‘ % name)
- parser = MyHTMLParser()
- parser.feed(‘‘‘<html>
- <head></head>
- <body>
- <!-- test html parser -->
- <p>Some <a href=\"#\">html</a> HTML tutorial...<br>END</p>
- </body></html>‘‘‘)
- feed()方法可以多次调用
Python学习笔记(二十二)
来源: http://www.bubuko.com/infodetail-2313499.html