当前位置: 首页 > news >正文

2016网站建设报价表360社区app

2016网站建设报价表,360社区app,哪些网站是做货源的,交互设计大学世界排名4-详细方法 | 用什么方法对文档树进行搜索?1 过滤器1.1 字符串1.2 正则表达式1.3 列表1.4 True1.5 可以自定义方法2 find_all()2.1 参数原型2.2 name参数2.3 keyword 参数2.4 string 参数2.5 limit 参数2.6 recursive 参数3 find()4 find_parents()和find_parent()5…

4-详细方法 | 用什么方法对文档树进行搜索?

  • 1 过滤器
    • 1.1 字符串
    • 1.2 正则表达式
    • 1.3 列表
    • 1.4 True
    • 1.5 可以自定义方法
  • 2 find_all()
    • 2.1 参数原型
    • 2.2 name参数
    • 2.3 keyword 参数
    • 2.4 string 参数
    • 2.5 limit 参数
    • 2.6 recursive 参数
  • 3 find()
  • 4 find_parents()和find_parent()
  • 5 find_next_siblings() 和 find_next_sibling()
  • 6 find_previous_siblings() 和 find_previous_sibling()
  • 7 find_all_next() 和 find_next()
  • 8 find_all_previous() 和 find_previous()
  • 9 本文涉及的源码

  • BeautifulSoup的文档搜索方法有很多,官方文档中重点介绍了两个方法:
find() 和 find_all() 
  • 下文中的实例,依旧是官网的例子:
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p><p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p><p class="story">...</p>
"""from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')

1 过滤器

  • 在介绍文档搜索方法之前,先了解下各种过滤器。

1.1 字符串

  • 即在搜索方法中传如一个字符串参数;
  • BeautifulSoup会查找与字符串完全匹配的内容;
  • 如查找b标签:
print(soup.find_all('b'))
  • 输出为:
[<b>The Dormouse's story</b>]

1.2 正则表达式

  • 传入正则表达式作为参数;
  • Beautiful Soup会通过正则表达式的 match() 来匹配内容;
  • 如找出所有以b开头的标签:
import re
for tag in soup.find_all(re.compile("^b")):print(tag.name)
  • 输出为:
body
b

1.3 列表

  • 传入列表参数;
  • Beautiful Soup会将与列表中任一元素匹配的内容返回;
  • 如找到文档中所有a标签和b标签:
print(soup.find_all(["a", "b"]))
  • 输出为:
[<b>The Dormouse's story</b>, 
<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, 
<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, 
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

1.4 True

  • True 可以匹配任何值;
  • 如查找到所有的tag
for tag in soup.find_all(True):print(tag.name)
  • 输出为:
html
head
title
body
p
b
p
a
a
a
p

1.5 可以自定义方法

  • 如果没有合适过滤器,那么还可以定义一个方法;
  • 方法只接受一个元素参数;
  • 如果这个方法返回 True 表示当前元素匹配并且被找到,如果不是则反回 False

2 find_all()

  • 搜索当前tag的所有tag子节点,并判断是否符合过滤器的条件。
  • 比如:
print(soup.find_all("title"))
  • 输出为:
[<title>The Dormouse's story</title>]

2.1 参数原型

find_all( name , attrs , recursive , string , **kwargs )

2.2 name参数

  • 查找所有名字为 nametag
  • 如:print(soup.find_all("title")),输出为:[<title>The Dormouse's story</title>]

2.3 keyword 参数

  • 如果一个指定名字的参数不是搜索内置的参数名,搜索时会把该参数当作指定名字tag的属性来搜索;
  • 如:print(soup.find_all(id='link2')),输出为:
[<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]
  • 按照CSS搜索,可以通过 class_ 参数搜索有指定CSS类名的tag
  • 如:print(soup.find_all("a", class_="sister")),输出为:
[<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

2.4 string 参数

  • 通过 string 参数可以搜文档中的字符串内容.name 参数的可选值一样;
  • 如:print(soup.find_all(string="Elsie")),输出为:['Elsie']

2.5 limit 参数

  • 可以使用 limit 参数限制搜索返回结果的数量,避免返回结果很大速度很慢;
  • 如:soup.find_all("a", limit=2),输出为:
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, 
<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]

2.6 recursive 参数

  • 只搜索tag的直接子节点,可以使用参数 recursive=False
  • 如:
<html><head><title>The Dormouse's story</title></head>
...
  • 不使用recursive 参数:
print(soup.html.find_all("title"))
  • 输出为:
[<title>The Dormouse's story</title>]
  • 使用recursive 参数:
print(soup.html.find_all("title", recursive=False))
  • 输出为:
[]

3 find()

  • find_all() 方法的返回结果是值包含一个元素的列表,而 find() 方法直接返回结果;
  • find_all() 方法没有找到目标是返回空列表, find() 方法找不到目标时,返回 None
  • 如:print(soup.find("nosuchtag")),输出为:None
  • 参数原型:
find( name , attrs , recursive , string , **kwargs )

4 find_parents()和find_parent()

  • 参数原型:
find_parents( name , attrs , recursive , string , **kwargs )
find_parent( name , attrs , recursive , string , **kwargs )
  • find_parents() 和 find_parent()用来搜索当前节点的父辈节点;
  • find_all() 和 find() 只搜索当前节点的所有子节点,孙子节点等;
  • 如:
a_string = soup.find(string="Lacie")
print(a_string)
print(a_string.find_parents("a"))
  • 输出为:
Lacie
[<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]

5 find_next_siblings() 和 find_next_sibling()

  • 参数原型:
find_next_siblings( name , attrs , recursive , string , **kwargs )
find_next_sibling( name , attrs , recursive , string , **kwargs )
  • 这2个方法通过 .next_siblings 属性对当tag的所有后面解析的兄弟tag节点进行迭代;
  • find_next_siblings() 方法返回所有符合条件的后面的兄弟节点;
  • find_next_sibling() 只返回符合条件的后面的第一个tag节点;
  • 如:
first_link = soup.a
print(first_link)
print(first_link.find_next_siblings("a"))
first_story_paragraph = soup.find("p", "story")
print(first_story_paragraph.find_next_sibling("p"))
  • 输出为:
<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
[<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
<p class="story">...</p>

6 find_previous_siblings() 和 find_previous_sibling()

  • 参数原型:
find_previous_siblings( name , attrs , recursive , string , **kwargs )
find_previous_sibling( name , attrs , recursive , string , **kwargs )
  • 这2个方法通过 .previous_siblings 属性对当前tag的前面解析的兄弟tag节点进行迭代;
  • find_previous_siblings() 方法返回所有符合条件的前面的兄弟节点;
  • find_previous_sibling() 方法返回第一个符合条件的前面的兄弟节点。

7 find_all_next() 和 find_next()

  • 参数原型:
find_all_next( name , attrs , recursive , string , **kwargs )
find_next( name , attrs , recursive , string , **kwargs )
  • 这2个方法通过 .next_elements 属性对当前tag的之后的tag和字符串进行迭代;
  • find_all_next() 方法返回所有符合条件的节点;
  • find_next() 方法返回第一个符合条件的节点。

8 find_all_previous() 和 find_previous()

  • 参数原型:
find_all_previous( name , attrs , recursive , string , **kwargs )
find_previous( name , attrs , recursive , string , **kwargs )
  • 这2个方法通过 .previous_elements 属性对当前节点前面的tag和字符串进行迭代;
  • find_all_previous() 方法返回所有符合条件的节点;
  • find_previous() 方法返回第一个符合条件的节点。

9 本文涉及的源码

# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2023/2/17 
# 文件名称:bs04.py
# 作用:beautifulsoup的应用
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelsonfrom bs4 import BeautifulSouphtml_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p><p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p><p class="story">...</p>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
# ====== 过滤器 ======
# 字符串
print(soup.find_all('b'))
# 正则表达式
import re
for tag in soup.find_all(re.compile("^b")):print(tag.name)
# 列表
print(soup.find_all(["a", "b"]))
# True
for tag in soup.find_all(True):print(tag.name)# ====== find_all() ======
print(soup.find_all("title"))
print(soup.find_all(id='link2'))
print(soup.find_all("a", class_="sister"))
print(soup.find_all(string="Elsie"))
print(soup.find_all("a", limit=2))
print(soup.html.find_all("title", recursive=False))# ====== find() ======
print(soup.find("nosuchtag"))
a_string = soup.find(string="Lacie")
print(a_string)
print(a_string.find_parents("a"))
first_link = soup.a
print(first_link)
print(first_link.find_next_siblings("a"))
first_story_paragraph = soup.find("p", "story")
print(first_story_paragraph.find_next_sibling("p"))
http://www.shuangfujiaoyu.com/news/8368.html

相关文章:

  • 武汉做便宜网站建设在线制作网页网站
  • 深圳营销型网站建设设计公司seo技术自学
  • 抖音短剧推广怎么做北京网优化seo优化公司
  • 宁夏网站建设活动宣传推广方案怎么写
  • 网站做字体滚动怎么做的公关公司排名
  • 家政网站建设方案口碑营销的前提及好处有哪些?
  • 简单网页的制作长沙官网seo技术厂家
  • 女頻做的最好的网站网页设计模板html代码
  • 小说网站的网编具体做哪些工作上优化seo
  • 厦门公司网站开发长沙建站优化
  • 网站建设中怎么写域名注册要多少钱
  • cms 政府网站 模板 div css百度账号管理
  • 互动网站建设多少钱沧州网络推广公司
  • 二手车网站的建设佛山优化推广
  • 做算命类网站违法吗天津seo网站管理
  • 印度网站后缀百度竞价的优势和劣势
  • 有没有专门做名片的网站打广告去哪个平台免费
  • 服装网站建设与规划搜索关键词的软件
  • 轻量应用服务器做网站苏州关键词seo排名
  • 个人网址是什么邯郸网站优化
  • 网站要做手机版怎么做促销活动推广方法有哪些
  • 北京网站建设 网站维护企业网络推广网站
  • 定制网站建设公司游戏推广公司靠谱吗
  • 校园网站制作模板网店推广网站
  • 嘉定区建设局网站蚁坊软件舆情监测系统
  • 西瓜网络深圳网站建设 东莞网站建设关键词排名优化是什么意思
  • 网站建设与信息安全培训小结无锡百度快照优化排名
  • 站酷网站的比赛网页谁做的安全优化大师下载
  • 用cms织梦做网站图文教程济南谷歌推广
  • 如何做网站压力测试宁波谷歌seo推广公司