Beautiful Soup 简明教程
Beautiful Soup - Comparing Objects
根据 BeautifulSoup,如果两个可导航字符串或标记对象表示相同的 HTML/XML 标记,则它们相等。
现在让我们看看下面的示例,其中两个 <b> 标记被视为相等,即使它们位于对象树的不同部分,因为它们都看起来像 "<b>Java</b>"。
Example
from bs4 import BeautifulSoup
markup = "<p>Learn <i>Python</i>, <b>Java</b>, advanced <i>Python</i> and advanced <b>Java</b>! from Tutorialspoint</p>"
soup = BeautifulSoup(markup, "html.parser")
b1 = soup.find('b')
b2 = b1.find_next('b')
print(b1== b2)
print(b1 is b2)
Example
from bs4 import BeautifulSoup
markup = "<p>Learn <i>Python</i>, <b>Java</b>, advanced <i>Python</i> and advanced <b>Java</b>! from Tutorialspoint</p>"
soup = BeautifulSoup(markup, "html.parser")
i1 = soup.find('i')
i2 = i1.find_next('i')
print(i1.string== i2.string)
print(i1.string is i2.string)