实例化beatifulesoup
时间: 2023-07-08 20:24:51 浏览: 42
好的,我来回答你的问题。实例化BeautifulSoup需要引入bs4库,然后使用bs4库中的BeautifulSoup类。下面是一个简单的示例代码:
```python
from bs4 import BeautifulSoup
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
```
以上代码中,我们首先引入了bs4库。然后,我们定义了一个包含HTML文档的字符串。接下来,我们使用BeautifulSoup类实例化了一个名为soup的对象,它包含了整个HTML文档的解析结果。其中,'html.parser'是一个解析器,它指定了使用Python内置的HTML解析器来解析HTML文档。