Beautiful Soup 简明教程
Beautiful Soup - Comparing Objects
根据 BeautifulSoup,如果两个可导航字符串或标记对象表示相同的 HTML/XML 标记,则它们相等。
As per the beautiful soup, two navigable string or tag objects are equal if they represent the same HTML/XML markup.
现在让我们看看下面的示例,其中两个 <b> 标记被视为相等,即使它们位于对象树的不同部分,因为它们都看起来像 "<b>Java</b>"。
Now let us see the below example, where the two <b> tags are treated as equal, even though they live in different parts of the object tree, because they both look like "<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)
Output
True
False
在以下示例中,比较了两个 NavigableString 对象。
In the following examples, tow NavigableString objects are compared.
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)