爬虫 | 理解BeautifulSoup的定位方法

常用过滤器介绍

find_all 过滤器

详情见官方文档关于find_all的描述和讲解

find过滤器

详情见官方文档关于find的描述和讲解

find_all_next过滤器

详情见官方文档关于find_all_next的描述和讲解

HTML解析分析

HTML示例

html_doc = """
<div class="item">
   <div class="pic">
       <em class="">1</em>
          <a href="https:xxx.com/123456">
		  	<img width="100" alt="肖申克的救赎" src="https:xxx.com/123456" class="img">
           </a>
    </div>
    <div class="info">
          <div class="hd">
               <a href="https:xxx.com/123456" class="title_info">
	               <span class="title">肖申克的救赎</span>
	               <span class="title"> / The Shawshank Redemption</span>
	               <span class="other"> / 月黑高飞(港)  /  刺激1995(台)</span>
               </a>
    </div>
</div>
"""

文档树分析

HTML代码文档结构如下所示。

  • div class =' item'

    • div class = 'pic'

      • em
      • a
        • img class = 'img'
    • div class = 'info'

      • div class = 'hd'
        • a class = 'title_info'
          • span class = 'title'
          • span class = 'title
          • span class = 'title

将上方代码按照顺序排列绘图后如图1所示:在这里插入图片描述

图1 文档结构示意图

爬虫目标分析

在这里插入图片描述
注意:我们要获取的是右边的a > span的值,而不是左边a 下面的值。

解析HTML

使用find_all过滤器查找span:

1 # 导包
2 from bs4 import BeautifulSoup
3 # 解析文档
4 soup = BeautifulSoup(html_doc, 'html.parser')
5 # 第一步:找到文档中所有的a,且class=’title_info‘
6 for card in soup.find_all ('a',class_='title_info'):
7 # 第二步:找到a下面的span
8     title = card.find_all('span')
9 # 第三步:迭代获取值
10     for i in title:
11        print (i.string.strip())
 -----------代码执行结果---------------
肖申克的救赎
/ The Shawshank Redemption
/ 月黑高飞()  /  刺激1995()

在上面代码中使用的是find_all过滤器,也可使用find_all_next过滤器,而使用find过滤器只能获取到第一个span(意味着不需要迭代)。

使用find_all_next过滤器:

6 for card in soup.find_all ('a',class_='title_info'):
7 # 第二步:找到a下面的span
8     title = card.find_all_next('span')
9 # 第三步:迭代取值
10     for i in title:
11        print (i.string.strip())
 -----------代码执行结果---------------
肖申克的救赎
/ The Shawshank Redemption
/ 月黑高飞()  /  刺激1995()

使用find过滤器:

6 for card in soup.find_all ('a',class_='title_info'):
7 # 第二步:找到a下面的span
8     title = card.find('span')
11    print (i.string.strip())
 -----------代码执行结果---------------
肖申克的救赎

HTML 结构再分析

在这里插入图片描述
事实上 第一层过滤条件可以从div class = 'info'开始过滤,也可以从div class = 'hd'开始过滤,关键在于第二层过滤的条件必须为span

还有一种情况,直接从a开始过滤(不限定条件),也可以返回正确的结果:

......
for card in soup.find_all('a'):
    title = card.find_all('span')
    for i in title:
        print (i.string.strip())
 -----------代码执行结果---------------
肖申克的救赎
/ The Shawshank Redemption
/ 月黑高飞()  /  刺激1995()

总结

第一层定位是一个相对定位,只要在你想要爬取的信息结构前面的标签(同时考虑便利情况),都可以使用一个标签定位(一般使用相邻的标签)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是希望

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值