python bs4(beautifulsoup4)

BS4本身是一种对描述语言进行封装的函数操作模块,通过提供面向对象的操作方式将文档对象中的各种节点、标签、属性、内容等等都封装成了python中对象的属性,在查询操作过程中,通过调用指定的函数直接进行数据 匹配检索操作,非常的简单非常的灵活。
一般BS4将HTML文档对象会转换成如下四种类型组合的文档树

  • Tag:标签对象
  • NavigableString:字符内容操作对象
  • BeautifulSoup:文档对象
  • Comment:特殊类型的NavigableString

获取标签内容

from bs4 import  BeautifulSoup

# 构造对象
soup = BeautifulSoup(open('westos.html','rb'), 'html.parser')
# 获取标签, 默认获取找到的第一个符合的内容
print(soup.title)
print(type(soup.title))
print(soup.p)

在这里插入图片描述

若出现错误UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xa2 in position 218: illegal multibyte sequence 错误的意思是:Unicode的解码(Decode)出现错误(Error)了,以gbk编码的方式去解码(该字符串变成Unicode),但是此处通过gbk的方式,却无法解码(can’t decode )。“illegal multibyte sequence”意思是非法的多字节序列,即没法(解码)了。
解决方法:在读取文本的时候加入参数‘b’,不会提示错误,通过输出读取的数据显示。

获取标签的属性

from bs4 import BeautifulSoup
soup = BeautifulSoup(open('westos.html','rb'),'html.parser')
print(soup.p.attrs)
# 获取标签指定属性的内容
print(soup.p['id'])
print(soup.p['class'])
print(soup.p['style'])

在这里插入图片描述

对标签属性进行修改

from bs4 import BeautifulSoup
soup = BeautifulSoup(open('westos.html','rb'),'html.parser')
print(soup.p)
soup.p['id'] = 'modifyid'
print(soup.p)

在这里插入图片描述

获取标签的文本内容

from bs4 import BeautifulSoup
soup = BeautifulSoup(open('westos.html','rb'),'html.parser')
print(dir(soup.title))
print(soup.title.text)
print(soup.title.string)
print(soup.title.name)
print(soup.head.title.string)

在这里插入图片描述

操作子节点

from bs4 import BeautifulSoup
soup = BeautifulSoup(open('westos.html','rb'),'html.parser')
print(soup.head.contents) # .contents 属性可以将 tag的子节点以列表的形式输出。
print(soup.head.children) #.children返回一个生成器,可以通过循环获取Tag的子节点
for el in soup.head.children:
    print(el)

在这里插入图片描述

面对对象的匹配

# # 查找指定的标签内容(指定的标签)
import re
from bs4 import BeautifulSoup
soup = BeautifulSoup(open('westos.html','rb'),'html.parser')
res1 = soup.find_all('p')
print(res1)
# # 查找指定的标签内容(指定的标签)--与正则的使用
res2 = soup.find_all(re.compile(r'd+'))
print(res2)

在这里插入图片描述

# # 对于正则表达式进行编译, 提高查找速率;
# pattern = r'd.+'
# pattern = re.compile(pattern)
# print(re.findall(pattern, 'dog hello d'))
import re

from bs4 import BeautifulSoup
soup = BeautifulSoup(open('westos.html','rb'),'html.parser')
# 详细查找标签
print(soup.find_all('p', id='test1'))
print(soup.find_all('p', id=re.compile(r'test\d{1}')))
print(soup.find_all('p', class_="class1"))
print(soup.find_all('p', class_=re.compile(r'class\d{1}')))
# 查找多个标签
print(soup.find_all(['p', 'div']))
print(soup.find_all([re.compile('^d'), re.compile('p')]))


# 内容的匹配
print(soup.find_all(text='文章标题'))
print(soup.find_all(text=re.compile('标题')))
print(soup.find_all(text=[re.compile('标题'), 'Title']))

在这里插入图片描述

CSS匹配

from bs4 import BeautifulSoup
soup = BeautifulSoup(open('westos.html','rb'),'html.parser')
# CSS常见选择器: 标签选择器(div), 类选择器(.class1), id选择器(#idname), 属性选择器(p[type="text"])
# 标签选择器(div)
res1 = soup.select("p")
print(res1)
# 类选择器(.class1)
res2 = soup.select(".class2")
print(res2)
# id选择器(#idname)
res3 = soup.select("#test1")
print(res3)
#  属性选择器(p[type="text"]
print(soup.select("p[id='test1']"))
print(soup.select("p['class']"))

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/zcx1203/article/details/83150420