bs4

bs4

说明:bs4是一个强大的解析工具,它借助网页的结构和属性等特性来解析网页。

bs4的代码非常简洁
示例:

from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
result = soup.tilte.string)

说明:html是被解析的html,result是解析的结果,title是html的标题,string是使得结果直接是title内的字符串,而不会包含html的标签等字符。
示例:

#不使用string结果
<title>The Dormouse's story</title>
#使用string的结果
The Dormouse's story

节点属性获取

说明:bs4可以反向获取对应节点的属性
示例,获取p标签的class属性:

方法一,使用attrs方法:

from bs4 import BeautifulSoup
html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story ppppp</b></p>
<p class="story">Once upon a time there were three little sister</p>
"""
soup = BeautifulSoup(html, 'lxml')

print(soup.p.attrs['class'])	#title

方法二,节点元素后加括号:

from bs4 import BeautifulSoup
html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story ppppp</b></p>
<p class="story">Once upon a time there were three little sister</p>
"""
soup = BeautifulSoup(html, 'lxml')

print(soup.p['class'])	#title

bs4的几个属性

注:以下表格中的示例均取材下面的示例。

名称功能示例示例说明特别说明
contents获取直接子节点soup.p.contents获取第一个p节点的所有内容
children获取直接子节点soup.p.children获取第一个p节点的所有内容contents和children的区别在于,content是得到的是一个列表,children得到的是一个迭代器
descendants获取所有子节点soup.p.descendants获取第一个p节点下的所有节点的内容descendants与children的区别在于,descendants列出了所包含的所有标签的标签节点及文本,children只列出了标签所含的内容
parents获取指定节点的父节点soup.a.parents获取a节点的父节点的全部内容返回的结果为生成器类型
neit_sibling获取节点的下一个兄弟元素soup.a.parents获取a节点的下一个同级的节点的内容
previous_siblings获取节点的上一个兄弟元素soup.a.previous_siblings获取a节点的上一个同级的节点的内容

示例代码:

from bs4 import BeautifulSoup
html = """
<html>
    <head>
        <title>The Dormouse's story</title>
    </head>
    <body>
        <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">
                <span>Elsie</span>
            </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, 'lxml')

方法选择器

find_all
说明:查询所有符合条件的元素。
语法:find_all(name,attrs,recursive,text,**kwargs)

其中(同样以上面代码为例):

名称说明示例示例说明
name查询参数soup.find_all(name=‘ul’)查询所有ul节点
attrs节点属性soup.find_all(attrs={‘id’:‘link1’})查询id属性为link1的节点
text匹配节点的文本,可以是字符串和正则soup.find_all(text=re.compile(‘link’))查询含有link文本信息的节点

其他类似方法:

名称说明
find()find查询第一个符合条件的元素
find_parents()返回所有祖先节点
find_parent()返回直接父节点
find_next()_siblings()返回后面所有兄弟节点
find_next()_sibling()返回后面的下一个兄弟节点
find_previous_siblings()返回前面的所有兄弟节点
find_previous_sibling()返回前面的上一个兄弟节点
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值