python学习爬虫(6)--BeautifulSoup搜索文档树:find_all(),find(),find_parents()等

本文介绍了Python爬虫中BeautifulSoup库的搜索文档树方法,包括find_all()、find()及其详细参数,如name、attrs、class_等,并提到了过滤器的多种类型,如字符串、正则表达式和自定义方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

作者:IT小样

通过之前的系列我们现在已经了解了BeautifulSoup的属性、操作,现在我们来一起学习一下如何搜索文档树。

1、find_all()方法

find_all()方法,找到所有匹配,关于传入参数,可以传入各种类型,接下来介绍不同过滤器。

1.1、过滤器

1.1.1、字符串过滤器

仍然以之前的html_doc为例,来进行示范:

html_doc = '''
<html><head><title>hello,tester</title></head><body>
<p class="title"><b><h1>Hello,welcome</h1></b></p>
<p class="documentation">Tester, welcome! This is a new partion of your job's life. With python, you can finnish your work easier and faster.How, <a href="http://example.com/easier" class="easier" id="link1"> easier </a> and <a href="http://example.com/faster" class="faster" id = "link2">faster</a> Now, you have a initial impression about python.</p>
<p class="documention">let's go!!!</p> 
</body></html>
'''
from bs4 import BeautifulSoup
sou
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值