爬虫学习笔记(二)——BeautifulSoup
来源:互联网 发布:windows系统日志备份 编辑:程序博客网 时间:2024/06/05 03:12
pip install beautifulsoup4
from bs4 import BeautifulSoup
基本元素:
tag 标签
Name tag.name 标签名
Attributes tag.attrs 标签属性,字典类型
NavigableString 标签内非属性字符串 tag.string
Comment 标签内字符串的注释部分
r = requests.get('url')
t = r.text
soup = BeautifulSoup(t, 'html.parser')
soup.a返回第一个a标签内容
soup.a.name a标签的名字
soup.a.parent.name a父标签的名字
soup.a.string a标签的非属性字符串
soup.a.contents 标签树下行遍历,保存子标签所有内容的列表
soup.a.children 子标签,与contents类似,迭代类型 for child in children:
soup.a.descendants 子孙标签,同上
上行遍历
soup.a.parent 节点的父亲标签,全部内容,包含a标签自己
soup.a.parents 先辈标签的迭代类型,用于循环
平行遍历:需在同一父节点下
.next_sibling 返回顺序的下一个平行节点标签
.previous_sibling 返回顺序的上一个平行节点标签
.next_siblings 返回顺序的后续所有平行节点标签
.previous_siblings 返回顺序的前续所有平行节点标签
print(soup.prettify())
按html格式显示
标签也可用同函数
soup.find_all('a') 检索标签名称为a的内容
soup.find_all(['a','b'])
<tag>.find_all(name, attrs, recursuve, string, **Kwargs)
name: 标签名
attrs: 标签属性值中的字符串。例如:'python', name='python', age=re.compile('1')
recursive: 是否对子孙标签检索,默认True
string: 非属性字符串内容搜索
<tag>(...) 等价于 <tag>.find_all(...)
soup(...) 等价于 soup.find_all(...)
其余方法:
.find() 搜索只返回一个结果,同.find_all()
.find_parents() 搜索前辈节点,返回列表类型
.find_parent() 返回一个先辈结果
.find_next_siblings() 搜索后续平行节点,返回列表类型
.find_next_siblings() 返回一个后续结果
.find_previous_siblings() 搜索前序平行节点,返回列表类型
.find_previous_siblings() 返回一个前序结果
soup.find_all(['a','b'])
<tag>.find_all(name, attrs, recursuve, string, **Kwargs)
name: 标签名
attrs: 标签属性值中的字符串。例如:'python', name='python', age=re.compile('1')
recursive: 是否对子孙标签检索,默认True
string: 非属性字符串内容搜索
<tag>(...) 等价于 <tag>.find_all(...)
soup(...) 等价于 soup.find_all(...)
其余方法:
.find() 搜索只返回一个结果,同.find_all()
.find_parents() 搜索前辈节点,返回列表类型
.find_parent() 返回一个先辈结果
.find_next_siblings() 搜索后续平行节点,返回列表类型
.find_next_siblings() 返回一个后续结果
.find_previous_siblings() 搜索前序平行节点,返回列表类型
.find_previous_siblings() 返回一个前序结果
阅读全文
0 0
- 爬虫学习笔记(二)——BeautifulSoup
- 【爬虫学习笔记】BeautifulSoup用法分析(二)
- Python爬虫包 BeautifulSoup 学习(二) 异常处理
- Python爬虫学习纪要(二):BeautifulSoup相关知识点2
- python学习笔记(二) BeautifulSoup
- 【爬虫学习笔记】BeautifulSoup用法分析(一)
- Python3爬虫学习笔记(4.BeautifulSoup库详解)
- python库学习笔记——爬虫常用的BeautifulSoup的介绍
- Python爬虫库学习笔记-BeautifulSoup
- python 网络爬虫学习笔记之beautifulsoup
- Python 3爬虫网易云(二)—— BeautifulSoup库用法上篇
- 网络爬虫之Beautifulsoup入门(二)
- Python 爬虫 —— BeautifulSoup
- 爬虫学习碎碎念——beautifulsoup,信息提取
- 学习笔记—Python网络爬虫(二)
- python beautifulsoup 爬虫学习
- 【爬虫学习】BeautifulSoup 学习心得
- 【Python3.6爬虫学习记录】(二)使用BeautifulSoup爬取简单静态网页文章
- Console类
- [笔试编程]2017.8.29腾讯模拟,满二叉排序树找最小子树
- HTML5:footer定位(底部+居中)的探讨+div图片居中问题
- 【C语言】【unix c】文件输入重定向
- 第七章 与Web集成(三) WEB INI配置
- 爬虫学习笔记(二)——BeautifulSoup
- MySQL索引
- 23种设计模式之——门面模式
- iOS 视频播放(AVPlayer、MPMoviePlayerController、MPMoviePlayerViewController 区别)
- 大话java之JDK动态代理
- CentOS配置MySQL基于日志点的复制Binary Log File Position Based Replication
- 使用SVN auto commit命令将本地文件同步到服务器
- Java 提高(3)----- 类
- 【C语言】【unix c】如何改变信号的处理方式