1. 使用内置的`open()`函数读取HTML文件内容:
with open('example.html', 'r') as file:html_content = file.read()print(html_content)
2. 使用`requests`库获取网页内容,然后使用`BeautifulSoup`库解析HTML:
import requestsfrom bs4 import BeautifulSoup获取网页内容response = requests.get('http://example.com')html_content = response.text使用BeautifulSoup解析HTMLsoup = BeautifulSoup(html_content, 'html.parser')print(soup.prettify())
3. 使用`lxml`库解析HTML:
from lxml import htmlwith open('example.html', 'r') as file:html_content = file.read()使用lxml解析HTMLtree = html.fromstring(html_content)print(tree.xpath('//html/body/h1/text()'))
以上方法可以帮助你读取HTML文件的内容。如果你需要提取特定的HTML元素或内容,可以使用`BeautifulSoup`库提供的各种方法和属性。

