我有一组 HTML 文件,我想提取每个文件中的第一个标签。由于文件 don\xe2\x80\x99t 具有特定标记,该标记始终是文件中的第一个,因此我\xe2\x80\x99m 不知道如何执行此操作。
\n\n例如,对于以下代码片段,第一个标签是<html>。
<html>\n <head>\n <title>\n insert title here\n </title>\n </head>\n</html>\nRun Code Online (Sandbox Code Playgroud)\n\n有什么办法可以用 BeautifulSoup (或者可能是其他工具)来完成这个任务?提前致谢 :)
\n在这种情况下,您可以使用BeautifulSoup,只需在对象find()上发出BeautifulSoup- 它会找到树中的第一个元素。.name会给你标签名称:
from bs4 import BeautifulSoup
data = """
<html>
<head>
<title>
insert title here
</title>
</head>
</html>
"""
soup = BeautifulSoup(data, "html.parser")
print(soup.find().name)
Run Code Online (Sandbox Code Playgroud)