python爬虫之数据解析(一):BeautifulSoup4库

本文介绍了BeautifulSoup4库在Python爬虫中的应用,包括安装、解析工具对比、常见对象、contents与children的区别、字符串相关属性、find_all和find方法的使用,以及CSS选择器的多种查找方式,如通过标签名、类名、ID等进行数据提取。
摘要由CSDN通过智能技术生成

BeautifulSoup4库

和 lxml 一样,Beautiful Soup 也是一个HTML/XML的解析器,主要的功能也是如何解析和提取 HTML/XML 数据。

安装和文档:

安装:
pip install bs4

中文文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html

几大解析工具对比:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qiAU6oRI-1599789551683)(C:\Users\HP\AppData\Roaming\Typora\typora-user-images\1554888897765.png)]

简单使用:

from bs4 import BeautifulSoup

html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><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,'lxml')
print(soup.prettify())

常见的四种对象:

  1. Tag:BeautifulSoup中所有的标签都是Tag类型,并且BeautifulSoup的对象其
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mercycrazy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值