爬虫学习碎碎念——beautifulsoup,信息提取
bs4解析提取html页面
遍历功能
.contents遍历前后父节点,孙节点
下行遍历
.children子节点
.descendants子孙节点
上行遍历
.parent当前节点父节点
.parents当前节点所有先辈节点
平行遍历
.next_sibling
.previous_sibling
.next_siblings
.previous_siblings
soup = BeautifulSoup(‘<p>data</p>’,"html.parser") #参数1:需要解析的html格式的信息,可以用变量。参数2:需要的解析器
加载解析器,解析相关代码
prettify(清晰的显示出标签来)
信息提取
信息标记:
Xml用<>标签 利用率不高,最复杂 网络上的信息交互与传递
Json有类型的键值对 双引号表示类型 移动应用云端和节点的信息通信,五注释
Yaml无类型键值对 简洁 ,文本信息比例最高 各类型配置文件,有注释
信息提取融合思路
for link in soup.find_all('a'):
print(link.get('href'))
for tag in soup.find_all(True):
print(tag.name)
html
head
title
body
p
b
p
a
a
检索所有信息中以b的信息
import re
for tag in soup.find_all(re.compile('b')):
print(tag.name)
find与find_all的用法http://www.jianshu.com/p/ef2f246cae46
查找p标签中包含course字符串的信息
soup.find_all('p','course')
name:对标签名称检索字符串
attrs:对标签属性值的检索字符串,可标注属性检索
recursive:是否对子孙全部检索,默认True
string:<>…</>中字符串区域的检索字符串
扩展方法