在Python 2.6上使用BeautifulSoup 4,需要先安装BeautifulSoup 4和html5lib库。以下是一个示例代码:
# 导入BeautifulSoup和html5lib库
from bs4 import BeautifulSoup
import html5lib
# 创建一个HTML字符串
html = """
示例网页
这是一个段落。
"""
# 使用BeautifulSoup解析HTML
soup = BeautifulSoup(html, 'html5lib')
# 打印解析结果
print(soup.prettify())
在Python 2.6中,需要先安装BeautifulSoup 4和html5lib库。可以通过以下命令安装:
pip install beautifulsoup4
pip install html5lib
安装完成后,运行上述代码即可在Python 2.6上使用BeautifulSoup 4解析HTML。