zoukankan      html  css  js  c++  java
  • 爬虫--数据解析方式

    引言:回顾requests实现数据爬取的流程

    1. 指定url
    2. 基于requests模块发起请求
    3. 获取响应对象中的数据
    4. 进行持久化存储

    其实,在上述流程中还需要较为重要的一步,就是在持久化存储之前需要进行指定数据解析。因为大多数情况下的需求,我们都会指定去使用聚焦爬虫,也就是爬取页面中指定部分的数据值,而不是整个页面的数据。因此,本次课程中会给大家详细介绍讲解三种聚焦爬虫中的数据解析方式。至此,我们的数据爬取的流程可以修改为:

    1. 指定url
    2. 基于requests模块发起请求
    3. 获取响应中的数据
    4. 数据解析
    5. 进行持久化存储

    三种数据解析方式

    1.正则
    2.bs4
    3.xpath

    1、正则

       单字符:
            . : 除换行以外所有字符
            [] :[aoe] [a-w] 匹配集合中任意一个字符
            d :数字  [0-9]
            D : 非数字
            w :数字、字母、下划线、中文
            W : 非w
            s :所有的空白字符包,括空格、制表符、换页符等等。等价于 [ f
    
    	v]。
            S : 非空白
        数量修饰:
            * : 任意多次  >=0
            + : 至少1次   >=1
            ? : 可有可无  0次或者1次
            {m} :固定m次 hello{3,}
            {m,} :至少m次
            {m,n} :m-n次
        边界:
            $ : 以某某结尾 
            ^ : 以某某开头
        分组:
            (ab)  
        贪婪模式: .*
        非贪婪(惰性)模式: .*?     在*或者+模式下加?就是懒惰模式
        re.I : 忽略大小写
        re.M :多行匹配
        re.S :单行匹配
    
        re.sub(正则表达式, 替换内容, 字符串)

    练习回顾

    import re
    #提取出python
    key="javapythonc++php"
    re.findall('python',key)[0]
    # print(type(s))===>返回的是列表<class 'list'>
    ['python', 'python']

    -----

    #提取出hello world
    key="<html><h1>hello world<h1></html>"
    # 分组用括号
    re.findall('<h1>(.*)<h1>',key)[0]
    'hello world'

    ---------------------

    #提取170
    string = '我喜欢身高为170的女孩'
    re.findall('d+',string)
    ---
    ['170']
    'd'    匹配数字0-9
    '+'     匹配前一个字符1次或多次,re.findall("ab+","ab+cd+abb+bba") 结果['ab', 'abb']

    -----------------------

    # 提取http:// 和https://
    key = 'http://www.baidu.com and https://boob.com'
    # '?'     匹配前一个字符1次或0次
    re.findall('https?',key)
    ------
    ['http', 'https']
    
    '?'     匹配前一个字符1次或0次 ,re.search('b?','alex').group() 匹配b 0次

    -----------------------

    # 提取出hit.
    key = 'bobo@hit.edu.com'
    
    # 是转义字符
    re.findall('h.*?.',key)# 贪婪模式:根据正则表达式尽可能多的提取数据
    # re.findall('hit',key) ===>['hit']
    ------------
    ['hit.']

    ----------------------

    #匹配出i开头的行 re.S(基于单行匹配)  re.M(基于多行)
    string = '''fall in love with you
    i love you very much
    i love she
    i love her'''
    
    re.findall('^i.*',string,re.M)
    ------------------
    ['i love you very much', 'i love she', 'i love her']

    --------------------------

    #匹配全部行
    string = """<div>静夜思
    窗前明月光
    疑是地上霜
    举头望明月
    低头思故乡
    </div>"""
    re.findall('<div>.*</div>',string,re.S)
    ----------------------
    ['<div>静夜思
    窗前明月光
    疑是地上霜
    举头望明月
    低头思故乡
    </div>']

     项目需求:爬取糗事百科指定页面的糗图,并将其保存到指定文件夹中 

    import requests
    import re
    # 指定url
    url = 'https://www.qiushibaike.com/pic/'
    
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
    
    # 发起请求
    response = requests.get(url=url,headers=headers)
    
    # 获取页面数据
    page_text = response.text
    
    # 数据解析(该列表中存储的就是当前页面源码中所有的图片链接)
    # 所有图片的模式相同
    # <div class="thumb">
    # <a href="/article/121316622" target="_blank">
    # <img src="//pic.qiushibaike.com/system/pictures/12131/121316622/medium/5JHF7G70C3OP2MYM.jpg" alt="一般都会说在外面过得很好">
    # </a>
    # </div>
    
    img_list = re.findall('<div class="thumb">.*?<img src="(.*?)".*?>.*?</div>',page_text,re.S)
    
    print(img_list)

     爬取当前url页面的数据

    import requests
    import re
    import os
    
    
    # 指定url
    url = 'https://www.qiushibaike.com/pic/'
    
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
    
    # 发起请求
    response = requests.get(url=url,headers=headers)
    
    # 获取页面数据
    page_text = response.text
    
    # 数据解析(该列表中存储的就是当前页面源码中所有的图片链接)
    # 所有图片的模式相同
    # <div class="thumb">
    # <a href="/article/121316622" target="_blank">
    # <img src="//pic.qiushibaike.com/system/pictures/12131/121316622/medium/5JHF7G70C3OP2MYM.jpg" alt="一般都会说在外面过得很好">
    # </a>
    # </div>
    
    img_list = re.findall('<div class="thumb">.*?<img src="(.*?)".*?>.*?</div>',page_text,re.S)
    
    # 创建一个存储图片数据的文件夹
    if not os.path.exists('./imgs'):
        os.mkdir('imgs')
        
    for url in img_list:
        img_url = 'https:'+url
        # 持久化处理
        img_data = requests.get(url=img_url,headers=headers).content
        imgName = url.split('/')[-1]
        withPath = 'imgs/'+imgName
        with open(withPath ,'wb')as f:
            f.write(img_data)
            print(imgName+"写入成功")

     爬取分页的数据

    # 获取图片数据做分页处理
    import requests
    import re
    import os
    # 指定url
    
    
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
    
    # 创建一个存储图片数据的文件夹
    if not os.path.exists('./imgs2'):
        os.mkdir('imgs2')
        
    # 指定页面
    start_page = int(input('start_page:'))
    end_page = int(input('end_page:'))    
        
    #循环解析且下载指定页码中的图片数据
    
    for page in range(start_page,end_page+1):
        print('正在下载第%d页图片'%page)
        # 区别
    #     params={'page':page}
        #  https://www.qiushibaike.com/pic/page/3/
        url = 'https://www.qiushibaike.com/pic/'
        new_url = url + str(page)
        print(new_url)
        
        # 发起请求
        response = requests.get(url=new_url,headers=headers)
        # 获取页面数据
        
        page_text = response.text
        img_list = re.findall('<div class="thumb">.*?<img src="(.*?)".*?>.*?</div>',page_text,re.S)
        for url in img_list:
            img_url = 'https:'+ url
        # 持久化处理
            img_data = requests.get(url=img_url,headers=headers).content
    
            imgName = url.split('/')[-1]
            withPath = 'imgs2/'+imgName
            
            with open(withPath ,'wb')as f:
                f.write(img_data)
                print(imgName+"写入成功")

    xpath在爬虫中的使用

    #### xpath在爬虫中的使用流程
        - 1.下载:pip install lxml
        - 2.导包:from lxml import etree
        - 3.创建etree对象进行指定数据的解析
            - 本地:etree = etree.parse(‘本地文件路径’)
                etree.xpath('xpath表达式')
            - 网络:etree=etree.HTML('网络请求到的页面数据')
                etree.xpath('xpath表达式')

    常用xpath表达式回顾

    ### 常用xpath表达式回顾
    属性定位:
        #找到class属性值为song的div标签
        //div[@class="song"] 
    层级&索引定位:
        #找到class属性值为tang的div的直系子标签ul下的第二个子标签li下的直系子标签a
        //div[@class="tang"]/ul/li[2]/a
    逻辑运算:
        #找到href属性值为空且class属性值为du的a标签
        //a[@href="" and @class="du"]
    模糊匹配:
        //div[contains(@class, "ng")]
        //div[starts-with(@class, "ta")]
    取文本:
        # /表示获取某个标签下的文本内容
        # //表示获取某个标签下的文本内容和所有子标签下的文本内容
        //div[@class="song"]/p[1]/text()
        //div[@class="tang"]//text()
    取属性:
        //div[@class="tang"]//li[2]/a/@href

    xpath()函数返回的是列表

    from lxml import etree
    
    # 1创建etree对象进行指定数据解析
    tree = etree.parse('./text.html')
    
    # 2调用xpath --》属性定位
    #tree.xpath('//div[@class="song"] ') ----tree.xpth()--->列表
    
    # 3层级索引定位
    #tree.xpath('//div[@class="tang"]/ul/li[2]/a')
    
    # 4逻辑定位
     #找到href属性值为空且class属性值为du的a标签
    #tree.xpath('//a[@href="" and @class="du"]')
    
    # 5模糊匹配
    # class 中有ng字符
    #tree.xpath('//div[contains(@class, "ng")]')
    #tree.xpath('//div[starts-with(@class, "ta")]')
    
    # 6取文本
    # /表示获取某个标签下的文本内容
    
    #tree.xpath('//div[@class="song"]/p[1]/text()')
    
    # //表示获取某个标签下的文本内容和所有子标签下的文本内容
    #tree.xpath('//div[@class="tang"]//text()')
    
    # 7取属性
    tree.xpath('//div[@class="tang"]//li[2]/a/@href')

    xpath项目需求:获取好段子中段子的内容和作者

     - xpath插件:就可以直接将xpath表达式作用到浏览器的网页当中
     - 安装:选择浏览器更多工具-扩展程序(管理)-打开开发者
         打开界面直接拖进来
     - 快捷键:
         开启和关闭xpath插件:ctrl+shitf+x
    ###  项目需求:获取好段子中段子的内容和作者   http://www.haoduanzi.com   

     

    import requests
    from lxml import etree
    # 1 指定url
    url = 'https://ishuo.cn/'
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
    # 2 发起请求
    response = requests.get(url=url,headers=headers)
    
    # 3.获取页面内容
    page_text = response.text
    
    # 4 数据解析 ,来源于本地用parse  ;来源于网络用HTML
    tree = etree.HTML(page_text)
    # 获取所有的li标签
    li_list = tree.xpath('//div[@id="list"]/ul/li')
    
    # 注意Element类型的对象可以继续调用xpath函数,对该对象表示的局部内容进行指定内容的解析
    # 5 持久化存储
    num = 0
    with open('./duanzi.text','w',encoding='utf-8')as f:
        
        for li in li_list:
            content = li.xpath('./div[@class="content"]/text()')[0]
            title = li.xpath('./div[@class="info"]/a/text()')[0]
            f.write(title+":"+content+"
    
    ")
            num = num +1
            print(f"第{num}条数据写入成功")
    
    

    Beautiful解析:

    - python独有。简单便捷和高效

    安装
    
    需要将pip源设置为国内源,阿里源、豆瓣源、网易源等
    
    windows (1)打开文件资源管理器(文件夹地址栏中) (2)地址栏上面输入 %appdata% (3)在这里面新建一个文件夹 pip 
    (4)在pip文件夹里面新建一个文件叫做 pip.ini ,内容写如下即可
    [global]
    timeout = 6000
    index-url = https://mirrors.aliyun.com/pypi/simple/
    trusted-host = mirrors.aliyun.com
    linux (
    1)cd ~ (2)mkdir ~/.pip (3)vi ~/.pip/pip.conf (4)编辑内容,和windows一模一样 需要安装:pip install bs4 bs4在使用时候需要一个第三方库,把这个库也安装一下 pip install lxml

    代码使用流程--Beatiful-属性和方法

    代码的使用流程
    -核心思想:将html文档转换成Beautifull对象,然后调用该对象中的属性和方法进行html文档指定内容的定位查找
    导包:from bs4 import BeatifulSoup
    创建Beatiful对象:
        -如果html文档来源于本地:
            BeautifulSoup(open('本地文件'), 'lxml')
        - 如果html是来源于网络
            Beatiful('网络请求到的页面数据','lxml')
    - 属性和方法
            基础巩固:
        (1)根据标签名查找
            - soup.a   只能找到第一个符合要求的标签
        (2)获取属性
            - soup.a.attrs  获取a所有的属性和属性值,返回一个字典
            - soup.a.attrs['href']   获取href属性
            - soup.a['href']   也可简写为这种形式
        (3)获取内容
            - soup.a.string
            - soup.a.text
            - soup.a.get_text()
           【注意】如果标签还有标签,那么string获取到的结果为None,而其它两个,可以获取文本内容
        (4)find:找到第一个符合要求的标签
            - soup.find('a')  找到第一个符合要求的
            - soup.find('a', title="xxx")
            - soup.find('a', alt="xxx")
            - soup.find('a', class_="xxx")
            - soup.find('a', id="xxx")
        (5)find_all:找到所有符合要求的标签
            - soup.find_all('a')
            - soup.find_all(['a','b']) 找到所有的a和b标签
            - soup.find_all('a', limit=2)  限制前两个
        (6)根据选择器选择指定的内容
                   select:soup.select('#feng')
            - 常见的选择器:标签选择器(a)、类选择器(.)、id选择器(#)、层级选择器
                - 层级选择器:
                    div .dudu #lala .meme .xixi  下面好多级
                    div > p > a > .lala          只能是下面一级
            【注意】select选择器返回永远是列表,需要通过下标提取指定的对象

    具体用法

    from bs4 import BeautifulSoup
    fp = open('text.html','r',encoding='utf-8')
    soup = BeautifulSoup(fp,'lxml')
    
    # (1)根据标签名查找--->只能找到第一个符合要求的标签
    #  soup.div
    
    #  (2)获取属性
    #     - soup.a.attrs  获取a所有的属性和属性值,返回一个字典
    #     - soup.a.attrs['href']   获取href属性
    #     - soup.a['href']   也可简写为这种形式   
        
    #soup.a.attrs
    #soup.a.attrs['href']
    
    #       (3)获取内容
    #         - soup.a.string       相当于xpath中的 /text()
    #         - soup.a.text         相当于xpath中的 //text()
    #         - soup.a.get_text()   相当于xpath中的 //text() 所有的 文本内容
    #        【注意】如果标签还有标签,那么string获取到的结果为None,而其它两个,可以获取文本内容 
    
    #soup.p.string
    
    #   (4)find:找到第一个符合要求的标签
    #         - soup.find('a')  找到第一个符合要求的
    #         - soup.find('a', title="xxx")
    #         - soup.find('a', alt="xxx")
    #         - soup.find('a', class_="xxx")
    #         - soup.find('a', id="xxx")
    
    #soup.find('a')
    #soup.find('div',class_='song')
    
    #   (5)find_all:找到所有符合要求的标签
    #         - soup.find_all('a')
    #         - soup.find_all(['a','b']) 找到所有的a和b标签
    #         - soup.find_all('a', limit=2)  限制前两个
    
    #soup.find_all('a')
    #soup.find_all('a',limit=2)
    
    # (6)根据选择器选择指定的内容
    #                select:soup.select('#feng')
    #         - 常见的选择器:标签选择器(a)、类选择器(.)、id选择器(#)、层级选择器
    #             - 层级选择器:
    #                 div .dudu #lala .meme .xixi  下面好多级
    #                 div > p > a > .lala          只能是下面一级
    #         【注意】select选择器返回永远是列表,需要通过下标提取指定的对象
    
    #soup.select('#feng')
    # select 函数的层级选择器 # 注意空格
    soup.select('li > a')

    使用bs4实现将诗词名句网站中三国演义小说的每一章的内容爬去到本地磁盘进行存储   

    诗词名句 

    from bs4 import BeautifulSoup
    import requests
    
    
    # 指定url
    url = 'http://www.shicimingju.com/book/sanguoyanyi.html'
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
    
    # 根据url获取页面内容中指定的标题所对应的文章内容
    def get_content(content_url):
        content_page = requests.get(url=content_url,headers=headers).text
        soup = BeautifulSoup(content_page,'lxml')
        div = soup.find('div',class_="chapter_content")
        # div tge类型对象
        return div.text
    
    # 发起请求
    response = requests.get(url=url,headers=headers)
    # 获取页面内容
    page_text = response.text
    
    soup = BeautifulSoup(page_text,'lxml')
    # a_list 存贮的是一系列的a标签对象
    a_list = soup.select('.book-mulu > ul > li > a')
    # print(a_list)
    # type(a_list[0])----bs4.element.Tag
    
    # 注意:Tag类型的对象可以继续调用响应的解析属性和方法进行局部数据的解析
    
    with open('./sanguo.md','w',encoding='utf-8')as f:
    
        for a in a_list:
            # 获取a标签的文本内容即---标题
            title = a.string
            # content_url = a['href']
            # content_url==> /book/sanguoyanyi/1.html
    
            content_url = 'http://www.shicimingju.com'+a['href']
    
        #    content_url===> http://www.shicimingju.com/book/sanguoyanyi/1.html
            # 获取章节内容内容
            content = get_content(content_url)
    #         print(title)
    #         print(content+'
    ')
            f.write(title+'
    '+content+'
    ')
            
        

    url:http://www.shicimingju.com/book/sanguoyanyi.html

    具体章节:http://www.shicimingju.com/book/sanguoyanyi/1.html

  • 相关阅读:
    Best Time to Buy and Sell Stock
    Remove Nth Node From End of List
    Unique Paths
    Swap Nodes in Pairs
    Convert Sorted Array to Binary Search Tree
    Populating Next Right Pointers in Each Node
    Maximum Subarray
    Climbing Stairs
    Unique Binary Search Trees
    Remove Duplicates from Sorted Array
  • 原文地址:https://www.cnblogs.com/foremostxl/p/10066312.html
Copyright © 2011-2022 走看看