在beautifulsoup中我们可以通过检索其标签名称得到对应的内容,也可以通过直接检索关键字得到内容,下面将进行实例:
import requests
from bs4 import BeautifulSoup
r=requests.get("https://www.baidu.com")
soup=BeautifulSoup(r.text,'html.parser')
1.find_all()
若要查找返回获取的网页结果中的所有a标签内容,则可以通过soup.find_all('a')得到所有的a标签内容。也可以获得多种标签的查找结果:soup.find_all(['a','b'])
![](https://img.haomeiwen.com/i14042963/9a8dab63133e5ccf.png)
也可以通过如图所示,打印出结果中的所有标签。另外在这个过程中我们也可以结合正则表达式来完成一定规则要求的查找。
![](https://img.haomeiwen.com/i14042963/402f400a51249682.png)
![](https://img.haomeiwen.com/i14042963/cc88d6e8ea80ae26.png)
![](https://img.haomeiwen.com/i14042963/0d4a6e3e0d519e80.png)
拓展方法:
![](https://img.haomeiwen.com/i14042963/1781a0432799a6cd.png)
因为find_all()函数在BeautifulSoup中太过常用,所以也设定了专门的简写
tag.fina_all()=tag(),soup.find_all()=soup()
网友评论