from sgmllib import SGMLParser, SGMLParseError ModuleNotFoundError: No module named 'sgmllib'
时间: 2023-10-12 17:10:43 浏览: 249
The `sgmllib` module has been deprecated since Python 2.6 and removed in Python 3.x. Instead, you can use the `html.parser` module, which provides similar functionality.
Here's an example of how to use `html.parser` to parse an HTML document:
```python
from html.parser import HTMLParser
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
print("Start tag:", tag)
for attr in attrs:
print(" attr:", attr)
def handle_endtag(self, tag):
print("End tag :", tag)
def handle_data(self, data):
print("Data :", data)
parser = MyHTMLParser()
parser.feed('<html><head><title>Test</title></head><body><h1>Header</h1><p>Paragraph</p></body></html>')
```
This will output:
```
Start tag: html
Start tag: head
Start tag: title
Data : Test
End tag : title
End tag : head
Start tag: body
Start tag: h1
Data : Header
End tag : h1
Start tag: p
Data : Paragraph
End tag : p
End tag : body
End tag : html
```
阅读全文