猫哥教你写爬虫 032--爬虫初体验-BeautifulSoup

本文详细介绍了如何使用BeautifulSoup库进行HTML解析,提取网页数据。通过实例演示了find()和find_all()方法的使用,以及如何处理Tag对象以获取所需信息。适合爬虫初学者学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

BeautifulSoup是什么

在爬虫中,需要使用能读懂html的工具,才能提取到想要的数据。这就是解析数据。
【提取数据】是指把我们需要的数据从众多数据中挑选出来。
解析与提取数据在爬虫中,既是一个重点,也是难点

BeautifulSoup怎么用

安装beautifulsoup ==> pip install BeautifulSoup4

在括号中,要输入两个参数,第0个参数是要被解析的文本,注意了,它必须必须必须是字符串。
括号中的第1个参数用来标识解析器,我们要用的是一个Python内置库:html.parser
import requests #调用requests库
res = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html') 
#获取网页源代码,得到的res是response对象
print(res.status_code) #检查请求是否正确响应
html = res.text #把res的内容以字符串的形式返回
print(html)#打印html
复制代码
import requests
from bs4 import BeautifulSoup
#引入BS库
res = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html') 
html = res.text
soup = BeautifulSoup(html,'html.parser') #把网页解析为BeautifulSoup对象
print(type(soup)) # <class 'bs4.BeautifulSoup'>
复制代码
打印soup出来的源代码和我们之前使用response.text打印出来的源代码是完全一样的
虽然response.textsoup打印出的内容表面上看长得一模一样,
它们属于不同的类:<class 'str'><class 'bs4.BeautifulSoup'>

提取数据

我们仍然使用BeautifulSoup来提取数据。
这一步,又可以分为两部分知识:find()find_all(),以及Tag对象

find()find_all()BeautifulSoup对象的两个方法,
它们可以匹配html的标签和属性,把BeautifulSoup对象里符合要求的数据都提取出来
区别在于,find()只提取首个满足要求的数据,而find_all()提取出的是所有满足要求的数据

localprod.pandateacher.com/python-manu…
HTML代码中,有三个<div>元素,用find()可以提取出首个元素,而find_all()可以提取出全部
import requests
from bs4 import BeautifulSoup
url = 'https://localprod.pandateacher.com/python-manuscript/crawler-html/spder-men0.0.html'
res = requests.get (url)
print(res.status_code)
soup = BeautifulSoup(res.text,'html.parser')
item = soup.find('div') #使用find()方法提取首个<div>元素,并放到变量item里。
print(type(item)) #打印item的数据类型
print(item)       #打印item 
复制代码
import requests
from bs4 import BeautifulSoup
url = 'https://localprod.pandateacher.com/python-manuscript/crawler-html/spder-men0.0.html'
res = requests.get (url)
print(res.status_code)
soup = BeautifulSoup(res.text,'html.parser')
items = soup.find_all('div') #用find_all()把所有符合要求的数据提取出来,并放在变量items里
print(type(items)) #打印items的数据类型
print(items)       #打印items
复制代码

举例中括号里的class_,这里有一个下划线,是为了和python语法中的类 class区分,避免程序冲突

小练习: 爬取网页中的三本书的书名、链接、和书籍介绍

localprod.pandateacher.com/python-manu…
另一个知识点——Tag对象。

我们一般会选择用type()函数查看一下数据类型,
Python是一门面向对象编程的语言,只有知道是什么对象,才能调用相关的对象属性和方法。

find()提取出来的数据类型和刚才一样,还是Tag对象

我们可以用Tag.text提出Tag对象中的文字,用Tag['href']提取出URL
import requests #调用requests库
from bs4 import BeautifulSoup
# 获取数据
res = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html') 
# res.status_code 状态码
# res.content 二进制
# res.text html代码
# res.encoding 编码
# 解析数据
# soup 是beautifulsoup对象
soup = BeautifulSoup(res.text,'html.parser')
# soup.find(标签名,属性=属性值)
# soup.find_all(标签名, 属性=属性值)
# 提取数据 list 里面是tag对象
item = soup.find_all('div',class_='books')
for i in item:
    # i.find().find().find() # tag对象, 可以一级一级找下去
    # i.find_all()
    # i 是tag对象, 也可以使用find和find_all, 得到结果还是tag对象
    # i.find().find().find().find()
    print(i.find('a',class_='title').text) # 获取标签内容
    print(i.find('a',class_='title')['href']) # 获取标签属性(href)
    print(i.find('p',class_='info').text) # 获取标签内容
复制代码
层层检索的过程有点像是在超市买你想要的零食

对象的变化过程

最开始requests获取数据,到BeautifulSoup解析数据,再用BeautifulSoup提取数据
不断经历的是我们操作对象的类型转换

完整版

完整版

再复习一遍代码...
import requests #调用requests库
from bs4 import BeautifulSoup
# 获取数据
res = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html') 
# res.status_code 状态码
# res.content 二进制
# res.text html代码
# res.encoding 编码
# 解析数据
# soup 是beautifulsoup对象
soup = BeautifulSoup(res.text,'html.parser')
# soup.find(标签名,属性=属性值)
# soup.find_all(标签名, 属性=属性值)
# 提取数据 list 里面是tag对象
item = soup.find_all('div',class_='books')
for i in item:
    # i.find().find().find() # tag对象, 可以一级一级找下去
    # i.find_all()
    # i 是tag对象, 也可以使用find和find_all, 得到结果还是tag对象
    # i.find().find().find().find()
    print(i.find('a',class_='title').text) # 获取标签内容
    print(i.find('a',class_='title')['href']) # 获取标签属性(href)
    print(i.find('p',class_='info').text) # 获取标签内容
复制代码

总结

beautifulsoup 解析器

解析器使用方法优势劣势
Python标准库BeautifulSoup(text, "html.parser")Python的内置标准库执行速度适中文档容错能力强Python 2.7.3 or 3.2.2前的版本中文档容错能力差
lxml HTML 解析器BeautifulSoup(text, "lxml")速度快文档容错能力强需要安装C语言库
lxml XML 解析器BeautifulSoup(text, "xml")速度快唯一支持XML的解析器需要安装C语言库
html5libBeautifulSoup(text, "html5lib")生成HTML5格式的文档速度慢不依赖外部扩展

作业1:爬取文章, 并保存到本地(每个文章, 一个html文件)

wordpress-edu-3autumn.localprod.forc.work

作业2: 爬取分类下的图书名和对应价格, 保存到books.txt

books.toscrape.com
最终效果...

快速跳转:

猫哥教你写爬虫 000--开篇.md
猫哥教你写爬虫 001--print()函数和变量.md
猫哥教你写爬虫 002--作业-打印皮卡丘.md
猫哥教你写爬虫 003--数据类型转换.md
猫哥教你写爬虫 004--数据类型转换-小练习.md
猫哥教你写爬虫 005--数据类型转换-小作业.md
猫哥教你写爬虫 006--条件判断和条件嵌套.md
猫哥教你写爬虫 007--条件判断和条件嵌套-小作业.md
猫哥教你写爬虫 008--input()函数.md
猫哥教你写爬虫 009--input()函数-人工智能小爱同学.md
猫哥教你写爬虫 010--列表,字典,循环.md
猫哥教你写爬虫 011--列表,字典,循环-小作业.md
猫哥教你写爬虫 012--布尔值和四种语句.md
猫哥教你写爬虫 013--布尔值和四种语句-小作业.md
猫哥教你写爬虫 014--pk小游戏.md
猫哥教你写爬虫 015--pk小游戏(全新改版).md
猫哥教你写爬虫 016--函数.md
猫哥教你写爬虫 017--函数-小作业.md
猫哥教你写爬虫 018--debug.md
猫哥教你写爬虫 019--debug-作业.md
猫哥教你写爬虫 020--类与对象(上).md
猫哥教你写爬虫 021--类与对象(上)-作业.md
猫哥教你写爬虫 022--类与对象(下).md
猫哥教你写爬虫 023--类与对象(下)-作业.md
猫哥教你写爬虫 024--编码&&解码.md
猫哥教你写爬虫 025--编码&&解码-小作业.md
猫哥教你写爬虫 026--模块.md
猫哥教你写爬虫 027--模块介绍.md
猫哥教你写爬虫 028--模块介绍-小作业-广告牌.md
猫哥教你写爬虫 029--爬虫初探-requests.md
猫哥教你写爬虫 030--爬虫初探-requests-作业.md
猫哥教你写爬虫 031--爬虫基础-html.md
猫哥教你写爬虫 032--爬虫初体验-BeautifulSoup.md
猫哥教你写爬虫 033--爬虫初体验-BeautifulSoup-作业.md
猫哥教你写爬虫 034--爬虫-BeautifulSoup实践.md
猫哥教你写爬虫 035--爬虫-BeautifulSoup实践-作业-电影top250.md
猫哥教你写爬虫 036--爬虫-BeautifulSoup实践-作业-电影top250-作业解析.md
猫哥教你写爬虫 037--爬虫-宝宝要听歌.md
猫哥教你写爬虫 038--带参数请求.md
猫哥教你写爬虫 039--存储数据.md
猫哥教你写爬虫 040--存储数据-作业.md
猫哥教你写爬虫 041--模拟登录-cookie.md
猫哥教你写爬虫 042--session的用法.md
猫哥教你写爬虫 043--模拟浏览器.md
猫哥教你写爬虫 044--模拟浏览器-作业.md
猫哥教你写爬虫 045--协程.md
猫哥教你写爬虫 046--协程-实践-吃什么不会胖.md
猫哥教你写爬虫 047--scrapy框架.md
猫哥教你写爬虫 048--爬虫和反爬虫.md
猫哥教你写爬虫 049--完结撒花.md

转载于:https://juejin.im/post/5cfc4adb6fb9a07eee5ec09a

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值