BeautifulSoup CSS 选择器——选择第 n 个孩子
在本文中,我们将看到如何使用beautifulsoup来选择 nth-child。为此,使用了模块的select()方法。 select() 方法使用 SoupSieve 包对解析的文档使用 CSS 选择器。
Syntax: select(“css_selector”)
CSS SELECTOR:
- nth-of-type(n): Selects the nth paragraph child of the parent.
- nth-child(n): Selects paragraph which is the nth child of the parent
方法:
- 导入模块
- 从网页中抓取数据。
- 将抓取的字符串解析为 HTML。
- 使用 find()函数获取具有给定类名或 id 或 tag_name 的标签。
- 使用 select(“css_selector”) 找到第 n 个孩子
- 打印孩子。
示例 1:
Python3
# importing module
from bs4 import BeautifulSoup
markup = """
GEEKS FOR GEEKS EXAMPLE
Geeks for Geeks
A Computer Science portal for geeks.
Heading
Programming Articles,
Programming Languages,
Quizzes;
practice
"""
# parsering string to HTML
soup = BeautifulSoup(markup, 'html.parser')
parent = soup.find(class_="coding")
# assign n
n = 2
# print the 2nd of parent
print(parent.select("b:nth-of-type("+str(n)+")"))
print()
# print the which is the 2nd child of the parent
print(parent.select("b:nth-child("+str(n)+")"))
Python3
# importing module
from bs4 import BeautifulSoup
import requests
# assign website
sample_website='https://www.geeksforgeeks.org/python-programming-language/'
page=requests.get(sample_website)
# parsering string to HTML
soup = BeautifulSoup(page.content, 'html.parser')
parent = soup.find(class_="wrapper")
# assign n
n = 1
# print the 2nd of parent
print(parent.select("b:nth-of-type("+str(n)+")"))
print()
# print the which is the 2nd child of the parent
print(parent.select("b:nth-child("+str(n)+")"))
输出:
解释:
- select("p:nth-of-type(n)") 表示选择父级的第 n 段子级。
- select(“p:nth-child(n)”) 表示选择段落,它是父级的第 n 个子级。
- 如果父级没有第 n 个子级,这两个函数都将返回 []。
示例 2:
蟒蛇3
# importing module
from bs4 import BeautifulSoup
import requests
# assign website
sample_website='https://www.geeksforgeeks.org/python-programming-language/'
page=requests.get(sample_website)
# parsering string to HTML
soup = BeautifulSoup(page.content, 'html.parser')
parent = soup.find(class_="wrapper")
# assign n
n = 1
# print the 2nd of parent
print(parent.select("b:nth-of-type("+str(n)+")"))
print()
# print the which is the 2nd child of the parent
print(parent.select("b:nth-child("+str(n)+")"))
输出: