在Python中调用HTML文件通常有以下几种方法:
1. 使用`open()`函数读取本地HTML文件内容:
with open('example.html', 'r') as file:
html_content = file.read()
print(html_content)
2. 使用`requests`库获取网页内容,然后使用`BeautifulSoup`库解析HTML:
import requests
from bs4 import BeautifulSoup
获取网页内容
response = requests.get('http://www.zyiz.net/python/features.html')
html_doc = response.text
解析HTML
soup = BeautifulSoup(html_doc, 'html.parser')
格式化并打印HTML内容
strhtm = soup.prettify()
print(strhtm)
3. 使用`webbrowser`模块打开HTML文件:
import webbrowser
打开HTML文件
webbrowser.open('example.html')
请根据您的需求选择合适的方法。如果您需要进一步处理HTML内容,比如提取特定数据,可以使用`BeautifulSoup`库提供的各种方法和属性