Python 3爬虫网易云(三)—— BeautifulSoup库用法下篇

上一篇演示了使用BeautifulSoup解析网页的HTML数据。今天演示如何使用BeautifulSoup模块来遍历HTML数据并提取我们想要的数据。


BeautifulSoup遍历方法

1>节点和标签名
可以使用子节点、父节点、 及标签名的方式遍历:

print(soup.title) #查找title标签
print(soup.a) #查找第一个a标签

#对标签的直接子节点进行循环
title_tag = soup.li
for child in title_tag.children:
    print(child)

soup.parent #父节点

# 所有父节点
link = soup.a
for parent in link.parents:
    if parent is None:
        print(parent)
    else:
        print(parent.name)

运行结果

<title>网易云音乐</title>
<a hidefocus="true" href="/#">网易云音乐</a>


<span><a data-module="discover" hidefocus="true" href="/#"><em>发现音乐</em><sub class="cor"> </sub></a></span>


h1
div
div
div
body
html
[document]

其次 兄弟节点操作为

# 兄弟节点
sibling_soup.b.next_sibling #后面的兄弟节点
sibling_soup.c.previous_sibling #前面的兄弟节点

#所有兄弟节点
for sibling in soup.a.next_siblings:
    print(repr(sibling))

for sibling in soup.find(id="link3").previous_siblings:
    print(repr(sibling))

搜索文档树

最常用的当然是find()和find_all()啦,当然还有其他的。比如find_parent() 和 find_parents()、 find_next_sibling() 和 find_next_siblings() 、find_all_next() 和 find_next()、find_all_previous() 和 find_previous() 等等。
我们就看几个常用的,其余的如果用到就去看官方文档哦。

BeautifulSoup官方文档
find_all()
搜索当前tag的所有tag子节点,并判断是否符合过滤器的条件。返回值类型是bs4.element.ResultSet。
完整的语法

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

网易云HTML代码过于复杂,这里用官方文档中的例子

soup.find_all("title")
# [<title>The Dormouse's story</title>]
#
soup.find_all("p", "title")
# [<p class="title"><b>The Dormouse's story</b></p>]
# 
soup.find_all("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>]
#
soup.find_all(id="link2")
# [<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]
#
import re
soup.find(string=re.compile("sisters"))
# u'Once upon a time there were three little sisters; and their names were\n'

name 参数:可以查找所有名字为 name 的tag。
attr 参数:就是tag里的属性。
string 参数:搜索文档中字符串的内容。
recursive 参数: 调用tag的 find_all() 方法时,Beautiful Soup会检索当前tag的所有子孙节点。如果只想搜索tag的直接子节点,可以使用参数 recursive=False 。

find()
与find_all()类似,只不过只返回找到的第一个值。返回值类型是bs4.element.Tag。
完整语法:

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

比如

soup.find('title')
# <title>The Dormouse's story</title>
#
soup.find("head").find("title")
# <title>The Dormouse's story</title>

这就是BeautifulSoup库的基本用法啦。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值