python笔记 爬虫精进·第2课 【BeautifulSoup模块,html.parser,解析数据,提取数据,find()与find_all(),Tag对象】

BeautifulSoup

BeautifulSoup库的应用,主要是爬虫的解析数据和提取数据。
安装方法:(win)pip install BeautifulSoup4 (mac)pip3 install BeautifulSoup4

解析数据

解析数据使用方法

from bs4 import BeautifulSoup
soup = BeautifulSoup(字符串,'html.parser') 

bs对象 = BeautifulSoup(要解析的文本,‘解析器’)
注意:
1.括号里面第0个参数是被解析的文本,必须是字符串
2.括号里第1个参数用来标识解析器,使用python内置库,html.parser
解析代码示例:

import requests
from bs4 import BeautifulSoup        #引入BeautifulSoup库
res = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html') 
html = res.text
soup = BeautifulSoup(html,'html.parser')    #把网页解析为BeautifulSoup对象

提取数据

提取数据仍然使用BeautifulSoup模块来进行

find()与find_all()

find()与find_all()是BeautifulSoup对象的两个方法,它们可以匹配html的标签和属性,把BeautifulSoup对象里符合要求的数据都提取出来。
find():只提取首个满足要求的数据
find_all():取出的是所有满足要求的数据
提取代码示例find(‘div’):

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()方法提取首个
元素,并放到变量item里。 print(type(item)) #打印item的数据类型 print(item) #打印item

提取代码示例find_all(‘div’):

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

二者区别:
1.find():运行结果是首个div元素。它的数据类型:,说明这是一个Tag类对象。
1.find_all():运行结果是三个div元素,它们一起组成了一个列表结构。它的数据类型是,是一个ResultSet类的对象。其实是Tag对象以列表结构储存了起来,可以把它当做列表来处理。
应用示例(提取网站书名、链接、书籍介绍)
(网址链接https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html)

import requests     # 调用requests库
from bs4 import BeautifulSoup       # 调用BeautifulSoup库
res = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html')       # 返回一个Response对象,赋值给res
html = res.text       # 把Response对象的内容以字符串的形式返回
soup = BeautifulSoup( html,'html.parser')      # 把网页解析为BeautifulSoup对象
items = soup.find_all(class_='books')         # 通过匹配标签和属性提取我们想要的数据
print(items) # 打印items
print(type(items)) #打印items的数据类型

用for循环遍历列表,提取三个div元素取出来了

import requests # 调用requests库
from bs4 import BeautifulSoup # 调用BeautifulSoup库
res = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html')# 返回一个Response对象,赋值给res
html= res.text             # 把Response对象的内容以字符串的形式返回
soup = BeautifulSoup( html,'html.parser') # 把网页解析为BeautifulSoup对象
items = soup.find_all(class_='books') # 通过定位标签和属性提取我们想要的数据
for item in items:
    print('想找的数据都包含在这里了:\n',item) # 打印item
Tag对象

提供有三种常用用法,具体用法如下
【Tag.find()和Tag.find_all()】:作用提取Tag中的Tag
【Tag.text】:作用提取Tag中的文字
【Tag[‘属性名’]】:输入参数属性名,可提取Tag中这个属性的值
依次提取Tag对象示例

import requests # 调用requests库
from bs4 import BeautifulSoup # 调用BeautifulSoup库
res = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html')       #返回一个response对象,赋值给res
html = res.text        #把res的内容以字符串的形式返回
soup = BeautifulSoup( html,'html.parser')        #把网页解析为BeautifulSoup对象
items = soup.find_all(class_='books') # 通过定位标签和属性提取我们想要的数据
for item in items:
    kind = item.find('h2') # 在列表中的每个元素里,匹配标签

提取出数据 title = item.find(class_='title') #在列表中的每个元素里,匹配属性class_='title'提取出数据 brief = item.find(class_='info') #在列表中的每个元素里,匹配属性class_='info'提取出数据 print(kind,'\n',title,'\n',brief) # 打印提取出的数据 print(type(kind),type(title),type(brief)) # 打印提取出的数据类型

备注:
1.除了拿到的数据之外;运行结果的数据类型,又是三个class ‘bs4.element.Tag’,用find()提取出来的数据类型和刚才一样,还是Tag对象。接下来要做的,就是把Tag对象中的文本内容提出来。
2.需要用到Tag对象的另外两种属性——Tag.text,和Tag[‘属性名’]
3.更改最后一行代码,提取信息,示例如下

import requests # 调用requests库
from bs4 import BeautifulSoup # 调用BeautifulSoup库
res =requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html')
# 返回一个response对象,赋值给res
html=res.text
# 把res解析为字符串
soup = BeautifulSoup( html,'html.parser')
# 把网页解析为BeautifulSoup对象
items = soup.find_all(class_='books')   # 通过匹配属性class='books'提取出我们想要的元素
for item in items:                      # 遍历列表items
    kind = item.find('h2')               # 在列表中的每个元素里,匹配标签

提取出数据 title = item.find(class_='title') # 在列表中的每个元素里,匹配属性class_='title'提取出数据 brief = item.find(class_='info') # 在列表中的每个元素里,匹配属性class_='info'提取出数据 print(kind.text,'\n',title.text,'\n',title['href'],'\n',brief.text) # 打印书籍的类型、名字、链接和简介的文字

至此,可以完整提取相应的标签内容

对象的变化过程

1.最开始用requests库获取数据,
2.第二步用BeautifulSoup库来解析数据
3.再继续用BeautifulSoup库提取数据python笔记 爬虫精进·第2课 【BeautifulSoup模块,html.parser,解析数据,提取数据,find()与find_all(),Tag对象】_第1张图片

你可能感兴趣的:(python学习笔记)