您的位置:首页 > 编程语言 > Python开发

北京理工大学-Python网络爬虫与信息提取学习笔记04

2020-04-22 04:13 537 查看

cmd命令 pip intsall beautifulsoup4

BeautifulSoup库是解析、遍历、维护“标签树”的功能库

使用BeautifulSoup库
from bs4 import BeautifulSoup
soup=BeautifulSoup(’< p>data</ p>,‘html.parser’)

BeautifulSoup对应一个HTML/XML文档的全部内容

BeautifulSoup库解析器
bs4的HTML解析器 BeautifulSoup(mk,‘html.parser’) 安装bs4库
lmxl的HTML解析器 BeautifulSoup(mk,‘lxml’) pip insatll lxml
lxml的XML解析器 BeautifulSoup(mk,‘xml’) pip install lxml
html5lib的解析器 BeautifulSoup(mk,‘html5lib’) pip install html5lib

BeautifulSoup类的基本元素
Tag 标签,最基本的信息组织单元,分别用<>和</>标明开头和结尾
Name 标签的名字,< p>…</ p>的名字是’p’,格式:< tag>.name
Attributes 标签的属性,字典形式组织,格式:< tag>.attrs
NavigableString 标签内非属性字符串,<>…</>中字符串,格式:< tag>.string
Comment 标签内字符串的注释部分,一种特殊的Comment类型

基于bs4库的HTML内容遍历方法

标签树的下行遍历
.contents 子节点的列表,将所有子节点列入列表
.children 子节点的迭代类型,与.contents类似,用于循环遍历子节点
遍历儿节点

for child in soup.body.childre:
print(child)

.descendants 子孙节点的迭代类型,包含所有子孙节点,用于循环遍历
遍历子孙节点

for child in soup.body.descendants:
print(child)

标签树的上行遍历
.parent 节点的父亲标签
.parents 节点先辈标签的迭代类型,用于循环遍历先辈节点

for parent in soup.a.parents:
if parent is None:
print(parent)
else:
print(parent.name)

标签树的平行遍历(平行遍历发生在同一个父节点下的各节点间)
.next_sibling 返回按照HTML文本顺序的下一个平行节点标签
.previous_sibling 返回按照HTML文本顺序的上一个平行节点标签
.next_siblings 返回按照HTML文本顺序的后续所有平行节点标签

for sibling in soup.a.next_siblings:
print(sibling)
.previous_siblings   返回按照HTML文本顺序的前续所有平行节点标签
for sibling in soup.a.previous_siblings:
print(sibling)

基于bs4库的HTML格式输出
bs4库的.prettify()方法

例子

import requests
from bs4 import BeautifulSoup

try:
r=requests.get('http://python123.io/ws/demo.html')
r.raise_for_status()
demo=r.text
soup=BeautifulSoup(demo,'html.parser')
print(soup.title)
tag=soup.a
print(tag.attrs)    #标签a的属性
print(tag.attrs['class'])
print(tag.attrs['href'])

print(type(tag.attrs))   #查看标签的类型
print(type(tag))   #tag标签可以有0个或多个属性

print(soup.p.string)   #查看标签的字符串
print(type(soup.p.string))

newsoup=BeautifulSoup('<b><!--This is a comment--></b><p>This is not a comment</p>','heml.parser')
print(newsoup.b.string)
print(type(newsoup.b.string))
print(newsoup.p.string)
print(type(newsoup.p.string))

print(tag)
print(soup.a.name)
print(soup.a.parent.name)
print(soup.a.parent.parent.naem)

print(soup.prettify())  #HTML格式化

except:
print('爬取失败')

HTML内容遍历

import requests
from bs4 import BeautifulSoup

try:
r=requests.get('http://python123.io/ws/demo.html')
r.raise_for_status()
demo = r.text
soup = BeautifulSoup(demo, 'html.parser')
print(soup.head.contents)
print(soup.body.contents)  #包括字符串和标签节点
print(len(soup.body.contents))  #body的节点数量
print(soup.body.contents[1])   #查看第二个节点

print(soup.title.parent)
print(soup.html.parent)   #html标签的父亲就是它自己
print(soup.parent)  #soup的父亲为空

print(soup.a.next_sibling)
print(soup.a.next_sibling.next_sibling)
print(soup.a.previous_sibling)
print(soup.a.previous_sibling.previous_sibling)

except:
print('爬取失败')

ps:学习链接 https://www.icourse163.org/learn/BIT-1001870001?tid=1206951268#/learn/announce

  • 点赞
  • 收藏
  • 分享
  • 文章举报
I_HAVE_COME 发布了23 篇原创文章 · 获赞 1 · 访问量 235 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: