看看下面的代碼:
from bs4 import BeautifulSoup
html = """
<h3 id="me"></h3>
<li id="test1"></li>
<li custom="test2321"></li>
<li id="test1" class="tester"></li>
<ul class="here"></ul>
"""
soup = BeautifulSoup(html)
# This tells BS to look at all the h3 tags, and find the ones that have an ID of me
# This however should not be done because IDs are supposed to be unique, so
# soup.find_all(id="me") should be used
one = soup.find_all("h3", {"id": "me"})
print one
# Same as above, if something has an ID, just use the ID
two = soup.find_all("li", {"id": "test1"}) # ids should be unique
print two
# Tells BS to look at all the li tags and find the node with a custom attribute
three = soup.find_all("li", {"custom": "test2321"})
print three
# Again ID, should have been enough
four = soup.find_all("li", {"id": "test1", "class": "tester"})
print four
# Look at ul tags, and find the one with a class attribute of "here"
four = soup.find_all("ul", {"class": "here"})
print four
輸出:
[<h3 id="me"></h3>]
[<li id="test1"></li>, <li class="tester" id="test1"></li>]
[<li custom="test2321"></li>]
[<li class="tester" id="test1"></li>]
[<ul class="here"></ul>]
This應提供必要的文件。
我只是把'attrs'一切:P。對我來說最簡單的方法:P –