BeautifulSoup库
1.下载BeautifulSoup库
在命令行输入pip install beautifulsoup4即可。
2.BeautifulSoup库的简单使用
from bs4 import BeautifulSoup #注意大小写
soup = BeautifulSoup('<p>data</p>','html.parser') #html.parser为解析器,后面会介绍
实例:
查看结果:(用soup.prettify())
3.BeautifulSoup库的解析器
4.bs4库的基本元素
使用举例:
5.bs4库的HTML遍历
分为下行遍历,上行遍历和平行遍历。
这里注意平行遍历前提是在同一个父节点的各节点间。
下行遍历:.contents , .children , .descendants (后两个用于for 循环)
上行遍历: .parent, .parents(.parents用于for 循环)
平行遍历: .next_sinling, .previous_sibling, .next_siblings , .precious_siblings(后两个用于for 循环)
总结:BeautifulSoup 基本入门知识get√