跟随B站波波老师学爬虫(三)——bf4数据解析

一、bf4数据解析原理

1、数据解析原理:

-标签定位
-提取解析标签属性存储的数据值

2、bs4数据解析原理:

  • 实例化beautifulSoup对象,并且将页面源码数据加载到该对象中
  • 通过调用beautifulSoup对象中相关的属性或者方法进行标签定位和数据提取

3、实例化的两种方法

  • 将本地html文档加载到该对象中

       fp=open('./exclusion_bs4_test.html','r',encoding='utf-8')
       soup=BeautifulSoup(fp,'lxml')
       print(soup)

  • 将互联网页面数据加载到对象中

       page_text=response.text
       soup=BeautifulSoup(page_text,'lxml')

4、根据页面元素的方法和属性定位

1)方法

  • soup.tagName:返回的是文档中第一次出现的tagName的标签
      tagName:div...
  • soup.find('div'):返回的是文档中第一次出现的div
  •  soup.find('div',class_/id/其他属性值='xx')定位class=xx/id=XX的div
  • soup.findall()  返回符合要求的所有标签
  • soup.select('某种选择器(id、class,标签选择器)'),返回的是一个列表
  • 层级选择器:soup.select('.xxxclassname>ul a')[i] >表示一个层级,空格表示多个层级

2)获取标签中文本数据属性值

  • soup.a标签.text、get_text()、string

   区别:前两个可以获取某一个标签中所有的文本内容,即使文本内容不属于该标签直系文本内容,而string只能获取该标签下直系的 文本内容

二、bs4数据解析实例分析

--需求:爬取三国演义小说所有章节标题和章节内容

https://www.shicimingju.com/book/sanguoyanyi.html

1、f12分析所有章节标题所在标签,在class=“book-mulu”,下的ul标签下的li标签下,获取这个文本数据就可以得到标题

所以写下来就是这样的

 url='https://www.shicimingju.com/book/sanguoyanyi.html'
  headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'}
# 拿到页面数据
  html_text=requests.get(url=url,headers=headers).text
# 数据解析,bs4 解析
# 实例化 beautifulsoup对象
  soup=BeautifulSoup(html_text,'lxml')
  li_list=soup.select('.book-mulu>ul>li')
 for li in li_list:
      # a标签直系文本内容,章节标题
      title=li.a.string

2、分析章节内容所在位置,如何解析

第一点:我们发现详情页的url是,我们获取到标题的href标签的链接

第二点:我们发现获取到整个页面元素后,只有获取到class=chapter_content标签下文本我们就可以爬取章节内容啦

 detail_url='https://www.shicimingju.com'+li.a['href']
      # 对详情页发起请求,请求章节内容
      detail_page_text=requests.get(url=detail_url,headers=headers).text
      # 解析内容
      detail_soup= BeautifulSoup(detail_page_text, 'lxml')
      div_tag=detail_soup.find('div',class_='chapter_content')
      # 解析章节内容
      content=div_tag.text

三、完整爬取代码

from bs4 import BeautifulSoup
import requests

if __name__ == '__main__':
  # 需求:爬取三国演义小说所有章节标题和章节内容
  url='https://www.shicimingju.com/book/sanguoyanyi.html'
  headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'}
# 拿到页面数据
  html_text=requests.get(url=url,headers=headers).text
# 数据解析,bs4 解析
# 实例化 beautifulsoup对象
  soup=BeautifulSoup(html_text,'lxml')
  li_list=soup.select('.book-mulu>ul>li')
  fp=open('./sanguo.txt','w',encoding='utf-8')
  for li in li_list:
      # a标签直系文本内容,章节标题
      title=li.a.string
      print(title)
      detail_url='https://www.shicimingju.com'+li.a['href']
      # 对详情页发起请求,请求章节内容
      detail_page_text=requests.get(url=detail_url,headers=headers).text
      # 解析内容
      detail_soup= BeautifulSoup(detail_page_text, 'lxml')
      div_tag=detail_soup.find('div',class_='chapter_content')
      # 解析章节内容
      content=div_tag.text
      print(content)
      fp.write(title+':'+content+'\n')
      print(title,"爬取成功!")

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值