html5制作网站谁的好,通化网站制作,wordpress 主题 百科,wordpress构建自己的网站#Beautiful Soup库中的find_all()方法是用于查找HTML文档中符合指定条件的所有元素。它返回一个列表#xff0c;其中包含了找到的所有元素。
# 对于Beautiful Soup库中的find_all()方法#xff0c;其参数类型可以分为以下几种#xff1a; # 标签名#xff1a;字符串类型其中包含了找到的所有元素。
# 对于Beautiful Soup库中的find_all()方法其参数类型可以分为以下几种 # 标签名字符串类型用于选择指定标签名的元素。例如p、a等。 # 属性名字符串类型用于选择具有指定属性的元素。例如class、id等。 # 属性值可以是字符串或正则表达式用于选择具有指定属性值的元素。可以使用字典形式传递例如{class: intro}表示选择具有class属性且属性值为intro的元素也可以使用正则表达式例如{href: ^https://}表示选择href属性以https://开头的元素。
# find_all()方法的基本语法如下 # find_all(name, attrs, recursive, string, limit, **kwargs) # 参数说明 # name可选参数用于指定要查找的标签名。 # attrs可选参数用于指定要查找的元素的属性或属性值。可以是一个字典或关键字参数形式。例如attrs{class: intro} 或 class_intro。 # recursive可选参数表示是否递归查找默认为True即在所有子孙节点中查找。soup.find_all(p, recursiveFalse) # string可选参数用于指定要查找的元素的文本内容。soup.find_all(p, stringThis is a regular paragraph.) # limit可选参数用于限制返回的结果数量。 soup.find_all(p, class_intro, limit2) # **kwargs关键字参数形式的属性查找例如 idmy-id # find_all()方法还支持传递任意的关键字参数来指定其他属性的查询条件。这些额外的关键字参数将被视为要匹配的元素的属性名和属性值。 # 例如如果要查找所有具有id属性且id属性值为content的元素可以使用以下方式 # elements soup.find_all(idcontent) # 同样如果要查找所有具有data-custom属性且属性值为123的元素可以使用以下方式 # elements soup.find_all(attrs{data-custom: 123})# 提示: 参数名class后面添加下划线_是因为class是Python中的保留关键字。
from bs4 import BeautifulSouphtml_doc
!DOCTYPE html
html
headtitleFind All Method Example/title
/head
bodyp classintroThis is the first paragraph./pp classintroThis is the second paragraph./pa hrefhttps://example.comLink 1/aa hrefhttps://example.comLink 2/adiv idcontent classmain-contenth2Title/h2pThis is a paragraph within the main content./p/div
/body
/html
soup BeautifulSoup(html_doc, html.parser)# 选择所有的 p 标签
p_elements soup.find_all(p)
for element in p_elements:print(element)print(- * 20)# 不递归查找只在直接子节点中查找 p 标签
p_elements_non_recursive soup.find_all(p, recursiveFalse)
for element in p_elements_non_recursive:print(element)print(- * 20)
# 查找文本内容为 This is a regular paragraph. 的 p 标签
p_element soup.find_all(p, stringThis is the first paragraph.)
print(p_element)print(- * 20)# 选择具有 class 属性且属性值为 intro 的元素
intro_elements soup.find_all(attrs{class: intro})
for element in intro_elements:print(element)print(- * 20)# 选择具有 href 属性且属性值以 https:// 开头的元素
link_elements soup.find_all(hrefTrue)
for element in link_elements:if element[href].startswith(https://):print(element)
select() 方法是 Beautiful Soup 库中用于使用 CSS 选择器选择元素的方法。它返回所有匹配选择器的元素列表。
select() 方法的基本语法如下 select(selector) 参数说明 selector要使用的 CSS 选择器可以是标签名、类名、ID、属性等。 select()方法的参数类型 # 标签选择器字符串类型如p、a等。 # 类class选择器字符串类型如.intro、.link等。 # ID选择器字符串类型如#header、#footer等。 # 属性选择器字符串类型如[href]、[src^https://]等。
# select 类选择器: 可以通过在选择器前加上点号来表示用于选择具有指定类名的元素。例如 # .intro选择类名为 intro 的元素。 # .link选择类名为 link 的元素。 # .active选择类名为 active 的元素。
# select ID选择器: 可以通过在选择器前加上井号来表示用于选择具有指定ID的元素。例如 # #header选择ID为 header 的元素。 # #footer选择ID为 footer 的元素。 # #nav选择ID为 nav 的元素。
# select 属性选择器: 可以使用方括号语法来表示用于选择具有指定属性或属性值的元素。例如 # [href]选择具有 href 属性的元素。 # [src]选择具有 src 属性的元素。 # [classintro]选择具有 class 属性且属性值为 intro 的元素。
from bs4 import BeautifulSouphtml_doc
!DOCTYPE html
html
headtitleCSS Class Selector Example/title
/head
bodydiv classintroh1Welcome to Beautiful Soup!/h1p classintroBeautiful Soup is a Python library for pulling data out of HTML and XML files./p/diva hrefhttps://chat18.aichatos.xyz/#/chat/1708763899782 classlinkClick me!/aulli classactiveItem 1/liliItem 2/lili classactiveItem 3/li/ul
/body
/html
soup BeautifulSoup(html_doc, html.parser)# 类选择器: 可以通过在选择器前加上点号来表示用于选择具有指定类名的元素# 选择类名为 intro 的元素
intro_elements soup.select(.intro)
for element in intro_elements:print(element)# 选择具有 class 属性且属性值为 intro 的元素
# intro_elements soup.select([classintro])
# for element in intro_elements:
# print(element)# 选择具有 href 属性的元素
href_elements soup.select([href])
for element in href_elements:print(element)# # 选择类名为 link 的元素
# link_elements soup.select(.link)
# for element in link_elements:
# print(element)# # 选择类名为 active 的元素
# active_elements soup.select(.active)
# for element in active_elements:
# print(element)