BeautifulSoup简介
BeautifulSoup可以用来从HTML或XML文件中提取数据
创建BeautifulSoup对象
- 第一种
直接通过字符串创建
soup = BeautifulSoup(html_str,'lxml',from_encoding="utf-8")
- 第二种
将字符串保存为html文件
soup = BeautifulSoup(open('index.html'))
- 打印
print(soup.prettify())
对象种类
- Tag
- NavigableString
- BeautifulSoup
- comment
1、Tag
示例:
2、 NavigableString
3、 BeautifulSoup
4、 comment
遍历文档树
1、子节点
2、父节点
3、兄弟节点
4、前后节点
搜索文档树