爬虫入门(五)——BeautifulSoup4

本文参考Beautiful Soup 4.2.0 文档,仅列举部分内容,详细内容见原文档,链接为:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id14

1. bs4简介

1.1 基本概念

BeautifulSoup 是⼀个可以从HTML或XML⽂件中提取数据的网页信息提取库(简称bs4),它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式

1.2 源码分析
  • github下载源码
  • 安装:
    pip install lxml(解析器)
    pip install bs4

2.bs4的用法在这里插入图片描述

2.1 bs4的对象种类
  • tag : 标签
  • NavigableString : 可导航的字符串
  • BeautifulSoup : bs对象
  • Comment : 注释
2.2 bs4的用法

直接上代码理解:

from bs4 import BeautifulSoup

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""

soup=BeautifulSoup(html_doc,'lxml')
print(soup.prettify())  #美化代码格式,使代码能按照标准的缩进格式的结构输出

print(soup.title)  #获取<title>标签:<title>The Dormouse's story</title>
print(soup.title.name)  #获取标签名称:title
print(soup.title.string)  #获取<title>标签中的文字:The Dormouse's story
print(soup.title.parent)  #获取<title>标签的父标签:<head><title>The Dormouse's story</title></head>
print(soup.title.parent.name)  #获取父标签名称:head

print(soup.p['class'])  #获取<p>标签中class属性值:['title']
print(soup.find_all('p'))  #获取<p>标签的所有内容,返回内容为列表形式

print(soup.a)  # 获取第一个<a>标签 
print(soup.find_all(id="link3")) #获取id="link3"的<a>标签,并返回列表形式:[<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

#找到所有<a>标签的链接
links=soup.find_all('a') #print(type(links)):<class 'bs4.element.ResultSet'>
for link in links:    #print(type(link)):<class 'bs4.element.Tag'>
    print(link['href']) #获取<a>标签中的链接

#获取所有文字内容转化为二进制形式
print(soup.get_text())

3.遍历树

3.1 遍历子节点
  • contents:返回的是⼀个列表
  • children:返回的是⼀个迭代器通过这个迭代器可以进⾏迭代
  • descendants:返回的是⼀个⽣成器遍历⼦⼦孙孙
  • string:获取标签⾥⾯的内容。如果一个tag仅有一个子节点,那么这个tag也可以使用 .string 方法,输出结果与当前唯一子节点的 .string 结果相同。果tag包含了多个子节点,tag就无法确定 .string 方法应该调用哪个子节点的内容
  • strings:返回是⼀个⽣成器对象⽤过来获取多个标签内容
  • stripped_strings:和strings基本⼀致但是它可以把多余的空格去掉

用代码展示:

#contents
head_tag=soup.head
print(head_tag)  #<head><title>The Dormouse's story</title></head>
print(head_tag.contents)  #[<title>The Dormouse's story</title>]
title_tag=head_tag.contents[0]
print(title_tag.contents)  #["The Dormouse's story"]

#children
for child in title_tag.children:
    print(child)  #The Dormouse's story

#descendants
for child in head_tag.descendants:
    print(child)  
    #<title>The Dormouse's story</title>
    #The Dormouse's story

# string
print(title_tag.string)  #The Dormouse's story
print(soup.html.string)  #tag包含多个子节点,无法确定调用哪个,输出结果为None

# strings
for string in soup.strings:
    print(string)
    print(repr(string))  #将字符串以标准形式返回,空格也以"/n"的形式出现
	#"The Dormouse's story"
    # '\n'
    # '\n'
    # "The Dormouse's story"
    # '\n'
    # 'Once upon a time there were three little sisters; and their names were\n'
    # 'Elsie'
    # ',\n'
    # 'Lacie'
    # ' and\n'
    # 'Tillie'
    # ';\nand they lived at the bottom of a well.'
    # '\n'
    # '...'
    # '\n'
    
#stripped_strings
for string in soup.stripped_strings:
    print(repr(string))
    # "The Dormouse's story"
    # "The Dormouse's story"
    # 'Once upon a time there were three little sisters; and their names were'
    # 'Elsie'
    # ','
    # 'Lacie'
    # 'and'
    # 'Tillie'
    # ';\nand they lived at the bottom of a well.'
    # '...'
    
#stripped_strings
for string in soup.stripped_strings:
    print(repr(string))
    # "The Dormouse's story"
    # "The Dormouse's story"
    # 'Once upon a time there were three little sisters; and their names were'
    # 'Elsie'
    # ','
    # 'Lacie'
    # 'and'
    # 'Tillie'
    # ';\nand they lived at the bottom of a well.'
    # '...'
3.2 遍历父节点
  • parent直接获得⽗节点
  • parents获取所有的⽗节点
3.3 遍历兄弟节点
  • next_sibling 下⼀个兄弟结点
  • previous_sibling 上⼀个兄弟结点
  • next_siblings 下⼀个所有兄弟结点
  • previous_siblings上⼀个所有兄弟结点

4. 搜索树

  • 字符串过滤器
  • 正则表达式过滤器
    我们⽤正则表达式⾥⾯compile⽅法编译⼀个正则表达式传给 find 或者findall这个⽅法可以实现⼀个正则表达式的⼀个过滤器的搜索
  • 列表过滤器
  • True过滤器
  • ⽅法过滤器
# 字符串过滤器
a_tag2 = soup.a
# 查找所有<b>标签
b_tag = soup.find_all('b')
print(b_tag)  #[<b>The Dormouse's story</b>]

# 正则表达式过滤器
import re
for tag in soup.find_all(re.compile('t')):
    print(tag.name)  
    #html                 
    #title

# 列表过滤器
print(soup.find_all(['p','a']))  #找到所有的p标签和a标签 
print(soup.find_all(['title','b']))  #[<title>The Dormouse's story</title>, <b>The Dormouse's story</b>]   

# True过滤器
for tag in soup.find_all(True):
    print(tag)  #层层递减,从最外层标签一直返回到最小标签
    print(tag.name)

#方法过滤器
def fn(tag):
    return tag.has_attr('class')
print(soup.find_all(fn)) #得到所有<p>标签

5. find_all() 和 find()

5.1 find_all()
  • find_all()⽅法以列表形式返回所有的搜索到的标签数据
  • find()⽅法返回搜索到的第⼀条数据

find_all()的方法参数:
deffind_all(self,name=None,attrs{},recursive=True,text=None,limit=None, kwargs)
name : tag名称
attr : 标签的属性
recursive : 是否递归搜索
text : ⽂本内容
limli : 限制返回条数
kwargs : 关键字参数

5.2 find_parents()、find_parent()find_next_siblings()、find_next_sibling()
  • find_parents() 搜索所有⽗亲
  • find_parrent() 搜索单个⽗亲
  • find_next_siblings()搜索所有兄弟
  • find_next_sibling()搜索单个兄弟
5.3 find_previous_siblings()、find_previous_sibling()、find_all_next()、find_next()
  • find_previous_siblings() 往上搜索所有兄弟
  • find_previous_sibling() 往上搜索单个兄弟
  • find_all_next()
  • 往下搜索所有元素 find_next()往下查找单个元素

6. 修改文档树

Beautiful Soup的强项是文档树的搜索,但同时也可以方便的修改文档树

  • 修改tag的名称和属性
  • 修改string 属性赋值,就相当于⽤当前的内容替代了原来的内容
  • append()向tag中添加内容,就好像Python的列表的 .append() ⽅法
  • decompose()修改删除段落,对于⼀些没有必要的⽂章段落我们可以给他删除掉
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值