BeautifulSoup的补充知识

目录

    • 四种类型
      • 1.Tag标签
      • NavigableString 标签中的内容(字符串)
      • BeautifulSoup表示整个文档
      • Comment也是输出内容,但是不显示注释
    • 对文档的遍历
    • 对文档的搜索
      • 第一种方式:find_all
      • 字符串过滤,匹配与字符串完全一样的内容
      • 正则表达式搜索
      • 方法:自定义函数进行搜索
      • 第二种:kwargs 参数
      • 第三种:text参数
      • 第四种:limit参数
      • css选择器

四种类型

在导入一个html文档后,可以通过BeautifulSoop进行解析,并将解析后的内容进行打印输出。
基本思路
引入bs4
然后打开要解析的网页,将网页内容赋给变量,然后通过BeautlfulSoup()进行解析,括号中要写入待解析的网页名称和解析器,如果是网页就用html.parser
再然后就可以对解析到的内容进行选择输出了。

1.Tag标签

from bs4 import BeautifulSoup
file = open("./baidu.html","rb")
html = file.read()
bs = BeautifulSoup(html,"html.parser")
#print(bs.title)
#print(bs.a)  #只能打印输出第一个符合条件的标签和内容
#print(bs.head)
print(type(bs.head))  #可以打印bs.head的类型,显示是Tag,一个标签

可以尝试输出html中的各种标签内容,在打印的结果处可以发现,他打印返回的是标签名及标签中的内容
在尝试打印标签的类型的时候,可以看到输出的就是Tag。

NavigableString 标签中的内容(字符串)

在获取到标签之后,还能继续对标签中的内容进行细分,并不是所有内容都是我们需要的,我们可以将标签进行拆分。
比如只输出标签中的内容,这时就可以用bs.head.string

from bs4 import BeautifulSoup
file = open("./baidu.html","rb")
html = file.read()
bs = BeautifulSoup(html,"html.parser")
print(bs.title)  #标签及内容
print(bs.title.string)  #标签中的内容
print(type(bs.title.string))  #NavigableString

经过打印输出可以发现bs.title输出了整个标签,而bs.title.string中只输出了标签中的内容,用type查看他的类型,显示的是NavigableString。
而我们要选择输出标签中的所有属性,可以使用bs.a.attrs

from bs4 import BeautifulSoup
file = open("./baidu.html","rb")
html = file.read()
bs = BeautifulSoup(html,"html.parser")
print(bs.a.attrs)  #打印出a中所有的属性,用字典键值对的形式存储

他会用字典存储所有的属性,属性名和具体内容分别是key和value

BeautifulSoup表示整个文档

当用BeautifulSoup解析文档后,再查看文档变量的格式会发现这时是BeautifulSoup格式。
对文档的名称进行输出会显示是document文件格式
将文档输出就会出现所有的内容。

from bs4 import BeautifulSoup
file = open("./baidu.html","rb")
html = file.read()
bs = BeautifulSoup(html,"html.parser")
#print(type(bs))  #类型就是bs4.BeautifulSoup
#print(bs.name)  #document
print(bs)  #将bs中的内容输出

Comment也是输出内容,但是不显示注释

选择输出文档中的内容,但是如果内容包括注释,这时注释不被显示,而显示的内容类型也变成了Comment。

from bs4 import BeautifulSoup
file = open("./baidu.html","rb")
html = file.read()
bs = BeautifulSoup(html,"html.parser")
print(bs.a.string)  #新闻
print(type(bs.a.string))  #Comment 是一个特殊的NavigableString输出的内容不包含注释符号

对文档的遍历

使用contents进行遍历文档内容。
整体思路就是通过BeautifulSoup对文档进行解析,然后输出文档中的内容contents,会用一个列表进行存储,每一行数据都存为列表中的一个元素,可以通过下标进行访问单个元素。

from bs4 import BeautifulSoup
file = open("./baidu.html","rb")
html = file.read()
bs = BeautifulSoup(html,"html.parser")
#print(bs.head.contents)  #用列表形式存储,每一行数据代表一个元素
print(bs.head.contents[1])  #所以可以进行下标查找

对文档的搜索

第一种方式:find_all

字符串过滤,匹配与字符串完全一样的内容

from bs4 import BeautifulSoup
file = open("./baidu.html","rb")
html = file.read()
bs = BeautifulSoup(html,"html.parser")
t_list = bs.find_all("a")
print(t_list)

bs.find_all(“a”)会查找与a完全一样的内容

正则表达式搜索

需要引入第三方库re

import re
from bs4 import BeautifulSoup
file = open("./baidu.html","rb")
html = file.read()
bs = BeautifulSoup(html,"html.parser")
t_list = bs.find_all(re.compile("a"))
print(t_list)  #找到含有a的全部输出

bs.find_all(re.compile(“a”))会输出所有带a的内容,只要有a就行,不用完全匹配。

方法:自定义函数进行搜索

定义一个函数,查找标签属性中含有某个值的内容,将包含的内容进行返回,打印输出。

from bs4 import BeautifulSoup
def name_is_exists(tag):
    return tag.has_attr("name")  #返回标签属性中带有name的内容
file = open("./baidu.html","rb")
html = file.read()
bs = BeautifulSoup(html,"html.parser")
t_list = bs.find_all(name_is_exists)
#print(t_list)
for item in t_list:
    print(item)

第二种:kwargs 参数

在bs.find_all()的括号中,输入要查找的内容,可以进行指定内容的查找输出。

from bs4 import BeautifulSoup
file = open("./baidu.html","rb")
html = file.read()
bs = BeautifulSoup(html,"html.parser")
#t_list = bs.find_all(id = "head")
#t_list = bs.find_all(class_=True) #class后边可以接内容
t_list = bs.find_all(href="http://news.baidu.com")  #查找指定内容
for item in t_list:
    print(item)

第三种:text参数

可以查找指定字符串,在bs.find_all()的括号中写入text=?表示要查找的内容,也可以查找多个内容,用列表进行存储。
还可以通过正则表达式进行搜索,在bs.find_all()括号中写入text = re.compile(),括号中输入要匹配的内容,在查找的时候,只要包含这个内容,就会把字符串输出 。
由于输出存储的是列表格式,可以通过for’循环进行格式输出。

from bs4 import BeautifulSoup
import re
file = open("./baidu.html","rb")
html = file.read()
bs = BeautifulSoup(html,"html.parser")
#t_list = bs.find_all(text = "hao123")  #找字符串
#t_list = bs.find_all(text = ["hao123","地图","贴吧"])
t_list = bs.find_all(text = re.compile("\d"))  #应用正则表达式查找包含特定文本的内容(标签中的字符串)
for item in t_list:
    print(item)

第四种:limit参数

在查找的时候,可能符合条件的内容有很多,但是我们不希望全部输出,而是只输出几条,那么我们就可以通过limit进行设置,limit=?就输出几条。

from bs4 import BeautifulSoup
def name_is_exists(tag):
    return tag.has_attr("name")  #返回标签属性中带有name的内容
file = open("./baidu.html","rb")
html = file.read()
bs = BeautifulSoup(html,"html.parser")
t_list = bs.find_all("a",limit=3)  #找3个标签是a的
for item in t_list:
    print(item)

css选择器

css选择器通过bs.select()方式进行查找,如果需要通过标签查找,就在bs.select()括号中填入要搜索的标签名称,在打印输出的时候就会把所有符合条件的标签进行打印输出,由于是列表存储,仍然可以通过for循环进行输出。
不仅可以通过标签进行搜索,也可以通过类名(.)、id、属性、或者子标签进行搜索查找。

from bs4 import BeautifulSoup
def name_is_exists(tag):
    return tag.has_attr("name")  #返回标签属性中带有name的内容
file = open("./baidu.html","rb")
html = file.read()
bs = BeautifulSoup(html,"html.parser")
#t_list = bs.select("title")  #用列表存储,根据标签查找
#t_list = bs.select(".mnav")  #不止能查找tag,根据类名查找。.表示类
#t_list = bs.select("#u1")  #通过id查找
#t_list = bs.select("a[class='bri']")  #通过属性查找
#t_list = bs.select("head > title")  #通过子标签查找
t_list = bs.select(".mnav ~ .bri")
print(t_list[0].get_text())  #打印文本信息

# for item in t_list:
#     print(item)

你可能感兴趣的:(python,开发语言,后端)