爬虫之数据解析

爬虫(Web Scraping)是一种自动从网站上提取信息的技术。数据解析(Data Parsing)则是爬虫过程中的一个关键步骤,它涉及到从爬取到的网页内容中提取出有用的数据。
数据解析的主流策略

  • 环境安装:

    • pip install bs4
    • pip install lxml
  • bs4数据解析的流程:

    • 创建一个BeautifulSoup对象,把被解析的数据加载到该对象中。
    • 调用BeautifulSoup对象相关的属性或者方法进行标签定位和数据提取
from bs4 import BeautifulSoup
fp = open('test.html','r')
#1.创建一个BeautifulSoup的工具对象,然后把即将被解析的页面源码数据加载到该对象中
#参数1:被解析的页面源码数据
#参数2:固定形式的lxml(一种解析器)
soup = BeautifulSoup(fp,'lxml')

#2.可以调用BeautifulSoup对象的相关函数和属性进行标签定位和数据提取

#标签定位-方式1:soup.tagName(只可以定位到第一次出现的该标签)
title_tag = soup.title
p_tag = soup.p

#标签定位-方式2(属性定位):soup.find(tagName,attrName='value')
#注意:find只可以定位满足要求的第一个标签,如果使用class属性值的话,find参数class_
#定位到了class属性值为song的div标签
div_tag = soup.find('div',class_='song')
#定位到class属性值为du的a标签
a_tag = soup.find('a',class_='du')
#定位到了id的属性值为feng的a标签
a_tag = soup.find('a',id='feng')

#标签定位-方式3(属性定位):soup.find_all(tagName,attrName='value')
#注意:find_all可以定位到满足要求的所有标签
tags = soup.find_all('a',class_='du')
#标签定位-方式4(选择器定位):
    #常用的选择器:class选择器(.class属性值)  id选择器(#id的属性值)
tags = soup.select('#feng') #定位到id的属性值为feng对应的所有标签
tags = soup.select('.du') #定位到class属性值为du对应的所有标签
#层级选择器:>表示一个层级  一个空格可以表示多个层
tags = soup.select('.tang > ul > li > a')
tags = soup.select('.tang a')
# print(tags)

#定位到标签内部数据的提取
#方式1:提取标签内的文本数据
    #tag.string:只可以将标签直系的文本内容取出
    #tag.text:可以将标签内部所有的文本内容取出
tag = soup.find('a',id='feng')
content = tag.string

div_tag = soup.find('div',class_='tang')
content = div_tag.text

#方式2:提取标签的属性值 tag['attrName']
img_tag = soup.find('img')
img_src = img_tag['src']
print(img_src)

案例应用:碧血剑文本爬取

  • url:https://bixuejian.5000yan.com/

  • 需求:将每一个章节的标题和内容进行爬取然后存储到文件中

import requests
from bs4 import BeautifulSoup

headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'
}

url = 'https://bixuejian.5000yan.com/'
#获取了首页对应的页面源码数据
response = requests.get(url=url,headers=headers)
response.encoding = 'utf-8'
page_text = response.text
#在首页页面源码数据中进行数据解析(章节的标题)
soup = BeautifulSoup(page_text,'lxml')
#所有的a标签定位保存到了a_list这个列表中
a_list = soup.select('.paiban > li > a')
for a in a_list:
    #章节的标题
    title = a.string
    detail_url = a['href'] #章节详情页的url
    #对详情页的url进行请求,为了获取详情页的页面源码数据,将章节内容进行解析
    detail_response = requests.get(url=detail_url,headers=headers)
    detail_response.encoding = 'utf-8'
    detail_page_text = detail_response.text
    #解析详情页,将章节内容进行提取
    detail_soup = BeautifulSoup(detail_page_text,'lxml')
    div_tag = detail_soup.find('div',class_='grap')
    #章节内容
    content = div_tag.text

    fileName = 'xiaoshuo/' + title + '.txt' #xiaoshuo/章节1.txt
    with open(fileName,'w') as fp:
        fp.write(title+'\n'+content)
    print(title,':爬取保存成功!')

xpath(重点)
  • 环境安装:

    • pip install lxml
  • xpath解析的编码流程:

    • 创建一个etree类型的对象,把被解析的数据加载到该对象中
    • 调用etree对象中xpath函数结合不同形式的xpath表达式进行标签定位和数据的提取
  • xpath表达式如何理解?

from lxml import etree
import requests
import os
#新建一个文件夹
dirName = 'girls'
if not os.path.exists(dirName):#如果文件夹不存在,则新建,否则不新建
    os.mkdir(dirName)

headers = {
    'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'
}

url = 'https://pic.netbian.com/4kmeinv/index.html'
response = requests.get(url=url,headers=headers)
response.encoding = 'gbk'
page_text = response.text

#数据解析:图片地址+图片名称
tree = etree.HTML(page_text)#HTML()专门用来解析网络请求到的页面源码数据
#该列表中存储的是每一个li标签
li_list = tree.xpath('//div[@class="slist"]/ul/li')
for li in li_list:
    #局部解析:将li标签中指定的内容解析出来
    img_title = li.xpath('./a/b/text()')[0]+'.jpg'# 左侧./表示xpath的调用者对应的标签
    img_src = 'https://pic.netbian.com'+li.xpath('./a/img/@src')[0]

    #对图片发起请求,存储图片数据
    img_data = requests.get(url=img_src,headers=headers).content
    # girls/123.jpg
    img_path = dirName + '/' + img_title
    with open(img_path,'wb') as fp:
        fp.write(img_data)
    print(img_title,'下载保存成功!')

爬取多页

from lxml import etree
import requests
import os
#新建一个文件夹
dirName = 'girls'
if not os.path.exists(dirName):#如果文件夹不存在,则新建,否则不新建
    os.mkdir(dirName)

headers = {
    'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'
}
#创建一个通用的url:除了第一页其他页码的通用url
url = 'https://pic.netbian.com/4kmeinv/index_%d.html'
for page in range(1,6):
    if page == 1:
        new_url = 'https://pic.netbian.com/4kmeinv/index.html'
    else:
        new_url = format(url%page)
    print('----------正在请求下载第%d页的图片数据----------'%page)
    response = requests.get(url=new_url,headers=headers)
    response.encoding =2 'gbk'
    page_text = response.text

    #数据解析:图片地址+图片名称
    tree = etree.HTML(page_text)#HTML()专门用来解析网络请求到的页面源码数据
    #该列表中存储的是每一个li标签
    li_list = tree.xpath('//div[@class="slist"]/ul/li')
    for li in li_list:
        #局部解析:将li标签中指定的内容解析出来
        img_title = li.xpath('./a/b/text()')[0]+'.jpg'# 左侧./表示xpath的调用者对应的标签
        img_src = 'https://pic.netbian.com'+li.xpath('./a/img/@src')[0]

        #对图片发起请求,存储图片数据
        img_data = requests.get(url=img_src,headers=headers).content
        # girls/123.jpg
        img_path = dirName + '/' + img_title
        with open(img_path,'wb') as fp:
            fp.write(img_data)
        print(img_title,'下载保存成功!')

相关推荐

  1. 爬虫数据

    2024-04-24 16:26:02       15 阅读
  2. json数据

    2024-04-24 16:26:02       7 阅读
  3. xml数据

    2024-04-24 16:26:02       6 阅读
  4. HDFS架构 数据架构

    2024-04-24 16:26:02       29 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-24 16:26:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-24 16:26:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-24 16:26:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-24 16:26:02       20 阅读

热门阅读

  1. 数仓建模—大数据建模

    2024-04-24 16:26:02       14 阅读
  2. 企业级开源版本管理系统GIT分析

    2024-04-24 16:26:02       11 阅读
  3. HTML超链接(详解如何进行网页之间的跳转)

    2024-04-24 16:26:02       13 阅读
  4. vue各种时间类型转换

    2024-04-24 16:26:02       11 阅读
  5. 达梦(DM)数据库管理表

    2024-04-24 16:26:02       11 阅读
  6. K8s: 控制器之ReplicaSet对象

    2024-04-24 16:26:02       11 阅读
  7. VMware虚拟机经常卡死风扇狂转的解决办法

    2024-04-24 16:26:02       13 阅读