【发布时间】:2018-03-21 21:40:10
【问题描述】:
如何使用 Beautiful Soup API 检查从同一个 BeautifulSoup 对象检索到的两个 Tag 或 NavigableString 对象是否真的是 DOM 中的同一个对象?
例如,下面最后一行代码的计算结果为 True,但我需要能够检测到 first_paragraph 和 second_paragraph 在 DOM 中不是同一个对象。
from bs4 import BeautifulSoup
example_html_str = """\
<html>
<head></head>
<body>
<p>Content</p>
<p>Content</p>
</body>
</html>
"""
soup = BeautifulSoup(example_html_str, "lxml")
paragraphs = soup.find_all("p")
first_paragraph = paragraphs[0] #First paragraph
second_paragraph = paragraphs[1] #Second paragraph
# The below evaluates to true, but they are not
# from the same part of the DOM.
print(first_paragraph == second_paragraph)
【问题讨论】:
标签: python beautifulsoup