BeautifulSoup的简单介绍及简单用法

BeautifulSoup的介绍及简单用法

1. 介绍

beautiful是一种用来解析html或xml文件并根据需要提取相关内容的python库,一般用来做数据挖掘(爬取数据,并解析数据)

from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')#,输入str,返回BeautifulSoup对象
#Beautiful Soup将复杂str转换成一个复杂的树形结构,每个节点都是Python对象,所有对象可以归纳为4种: Tag , NavigableString , BeautifulSoup , Comment .
type(soup)
#bs4.BeautifulSoup
print(soup.prettify())#使用prettify方法将字符串解析成为标准缩进html格式内容输出,输出结果还是string
type(soup.prettify())

结果:html标签是按树结构存储的

<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 class="sister" href="http://example.com/elsie" id="link1">
    Elsie
   </a>
   ,
   <a class="sister" href="http://example.com/lacie" id="link2">
    Lacie
   </a>
   and
   <a class="sister" href="http://example.com/tillie" id="link3">
    Tillie
   </a>
   ;
and they lived at the bottom of a well.
  </p>
  <p class="story">
   ...
  </p>
 </body>
</html>

在这里插入图片描述

2.一些soup对象的小操作

soup.title#返回第一个title,包括他自身(标签(name),内容(string)以及它所有的子节点(标签,内容)
#<title>The Dormouse's story</title>
soup.title.name#第一个title节点的标签名字
#'title'
soup.title.string#第一个title节点的内容
#"The Dormouse's story"
#返回一个节点,是返回这个节点及其包含的所有子节点
soup.title.parent#第一个title节点的父节点(返回一个节点,是返回这个节点及其包含的所有子节点)
#<head><title>The Dormouse's story</title></head>
soup.title.parent.name#第一个title节点的父节点的标签名字
#'head'
soup.title.parent.string
#"The Dormouse's story"
soup.p
#<p class="title"><b>The Dormouse's story</b></p>
soup.p['class']#第一个p节点的class属性值
#['title']
soup.a
#<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
#为a的节点
soup.find_all('a')#返回所有标签为a的节点,返回节点的列表
#[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
 #<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
 #<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
soup.find(id="link3")
#<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
print(soup.get_text())#从文档中获取文字内容(除去标签,单纯返回所有内容)

#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;
#and they lived at the bottom of a well.
#...

3.节点内部的理解

节点属性:类似于字典类型:标签属性为键,属性值为值d[key],d.get(key)
节点标签:link.name
节点内容:link.string

soup.a
#<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
soup.a.attrs#返回属性字典
#{'href': 'http://example.com/elsie', 'class': ['sister'], 'id': 'link1'}
for link in soup.find_all('a'):
    print(link['href'])#print(link.get('href'))
    print(link.name)
    print(link.string)
#http://example.com/elsie
#a
#Elsie
#http://example.com/lacie
#a
#Lacie
#http://example.com/tillie
#a
#Tillie
  • 1
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
BeautifulSoup是一个Python库,用于从HTML和XML文件中提取数据。它提供了一些简单而灵活的API,可以让你轻松地遍历HTML和XML文件,找到你需要的数据。 使用BeautifulSoup需要先安装该库。可以使用以下命令在命令行中安装: ``` pip install beautifulsoup4 ``` 以下是BeautifulSoup的基本用法: 1. 导入库 ``` from bs4 import BeautifulSoup ``` 2. 创建BeautifulSoup对象 创建一个BeautifulSoup对象,将HTML或XML文件作为参数传递给它。 ``` soup = BeautifulSoup(html_doc, 'html.parser') ``` 3. 标签选择器 使用标签选择器可以选择HTML或XML文件中的特定标签。 ``` soup.title # 选择<title>标签 soup.a # 选择<a>标签 ``` 4. 标签内容 使用`.string`属性可以获取标签的内容。 ``` soup.title.string # 获取<title>标签的内容 soup.a.string # 获取<a>标签的内容 ``` 5. 标签属性 使用`.attrs`属性可以获取标签的属性。 ``` soup.a.attrs # 获取<a>标签的所有属性 soup.a['href'] # 获取<a>标签的href属性 ``` 6. 标签嵌套 使用`.contents`属性可以获取标签的所有子标签,返回一个列表。 ``` soup.body.contents # 获取<body>标签的所有子标签 ``` 7. 标签搜索 使用`find()`或`find_all()`方法可以搜索HTML或XML文件中的标签。 ``` soup.find('a') # 查找第一个<a>标签 soup.find_all('a') # 查找所有<a>标签 ``` 8. CSS选择器 使用CSS选择器可以选择HTML或XML文件中的特定标签。 ``` soup.select('title') # 选择<title>标签 soup.select('a') # 选择<a>标签 ``` 以上是BeautifulSoup的基本用法,更多详细信息可以查看官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值