上一篇
python读取html指定内容
- 行业动态
- 2024-05-23
- 4
要使用Python读取HTML指定内容,可以使用BeautifulSoup库。首先安装库,然后导入库,最后使用BeautifulSoup解析HTML文件并提取
指定内容。
要使用Python读取HTML指定内容,可以使用BeautifulSoup库,首先需要安装BeautifulSoup库和lxml解析器:
pip install beautifulsoup4 pip install lxml
接下来,可以使用以下代码读取HTML文件中的指定内容:
from bs4 import BeautifulSoup 读取HTML文件 with open("example.html", "r", encoding="utf8") as file: html_content = file.read() 使用BeautifulSoup解析HTML soup = BeautifulSoup(html_content, "lxml") 查找指定的小标题和单元表格 h2_tags = soup.find_all("h2") table_tags = soup.find_all("table") 输出结果 print("小标题:") for h2 in h2_tags: print(h2.text) print(" 单元表格:") for table in table_tags: print(table)
将example.html
替换为你要读取的HTML文件名,这段代码会找到所有的<h2>
标签和小标题,以及所有的<table>
标签和单元表格。