美文网首页
BeautifulSoup

BeautifulSoup

作者: 遇明不散 | 来源:发表于2019-07-15 14:24 被阅读0次

BeautifulSoup

定义

HTML或XML的解析器,依赖于lxml

安装
python -m pip install beautifulsoup4
使用流程
# 导入模块
from bs4 import BeautifulSoup  
# 创建解析对象
soup = BeautifulSoup(html,'lxml')
# 查找节点对象
soup.find_all(name="属性值")
BeautifulSoup支持的解析库
  • lxml:BeautifulSoup(html,'lxml') 速度快,文档容错能力强
  • BeautifulSoup(html,'html.parser') python标准库,速度一般
  • xml:BeautifulSoup(html,'xml') 速度快,文档容错能力强
节点选择器
# 选择节点
soup.节点名
# 获取文本内容
soup.节点名.string
常用方法
# find_all():返回列表
r_list = soup.find_all(属性名="属性值")
r_list = soup.find_all(class_="test")

r_list = soup.find_all("节点名",attrs={"名":"值"})
r_list=soup.find_all("div",attrs={"class":"test"}

相关文章

网友评论

      本文标题:BeautifulSoup

      本文链接:https://www.haomeiwen.com/subject/wmqwkctx.html