可以使用BeautifulSoup库来解析HTML文档并选择其中的元素。 首先,需要安装BeautifulSoup库,可以通过以下命令实现: pip install beautifulsoup4
接下来,在python文件中引入BeautifulSoup库: from bs4 import BeautifulSoup
然后,读取HTML文档: with open("example.html") as fp: soup = BeautifulSoup(fp, "html.parser")
在soup对象上使用select()方法,选择元素:
paras = soup.select("p")
divs = soup.select("div.example")
header = soup.select_one("#header")
links = soup.select('a[href*="example.com"]')
更多关于BeautifulSoup的使用,可以参考官方文档。