• 三种数据解析方式学习


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

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

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

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

    一.正则解析

    常用正则表达式回顾:

    单字符:
    
            . : 除换行以外所有字符
    
            [] :[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} :量词可以重复前面匹配的字符n-m次,至少n次,最多m次。
    
        边界:
    
            $ : 以某某结尾 
    
            ^ : 以某某开头
    
        分组:
    
            (ab)  
    
        贪婪模式: .*
    
        非贪婪(惰性)模式: .*?
    
        re.I : 忽略大小写
    
        re.M :多行匹配
    
        re.S :单行匹配
    
        re.sub(正则表达式, 替换内容, 字符串)
    

     回顾练习:

    import re
    
    #提取出python
    
    key="javapythonc++php"
    
    re.findall('python',key)[0]
    
    #####################################################################
    
    #提取出hello world
    
    key="<html><h1>hello world<h1></html>"
    
    re.findall('<h1>(.*)<h1>',key)[0]
    
    #####################################################################
    
    #提取170
    
    string = '我喜欢身高为170的女孩'
    
    re.findall('d+',string)
    
    #####################################################################
    
    #提取出http://和https://
    
    key='http://www.baidu.com and https://boob.com'
    
    re.findall('https?://',key)
    
    #####################################################################
    
    #提取出hello
    
    key='lalala<hTml>hello</HtMl>hahah' #输出<hTml>hello</HtMl>
    
    re.findall('<[Hh][Tt][mM][lL]>(.*)</[Hh][Tt][mM][lL]>',key)
    
    #####################################################################
    
    #提取出hit. 
    
    key='bobo@hit.edu.com'#想要匹配到hit.
    
    re.findall('h.*?.',key)
    
    #####################################################################
    
    #匹配sas和saas
    
    key='saas and sas and saaas'
    
    re.findall('sa{1,2}s',key)
    
    #####################################################################
    
    #匹配出i开头的行
    
    string = '''fall in love with you
    
    i love you very much
    
    i love she
    
    i love her'''
    
    re.findall('^i.*',string,re.M)
    
    #####################################################################
    
    #匹配全部行
    
    string1 = """<div>静夜思
    
    窗前明月光
    
    疑是地上霜
    
    举头望明月
    
    低头思故乡
    
    </div>"""
    
    re.findall('.*',string1,re.S)
    

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

    #!/usr/bin/env python
    
    # -*- coding:utf-8 -*-
    
    import requests
    
    import re
    
    import os
    
    if __name__ == "__main__":
    
         url = 'https://www.qiushibaike.com/pic/page/%s/'
    
         headers={
    
             'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
    
         }
    
         #指定起始也结束页码
    
         page_start = int(input('enter start page:'))
    
         page_end = int(input('enter end page:'))
    
         #创建文件夹
    
         if not os.path.exists('./images'):
    
             os.mkdir('./images')
    
         #循环解析且下载指定页码中的图片数据
    
         for page in range(page_start,page_end+1):
    
             print('正在下载第%d页图片'%page)
    
             new_url = format(url % page)
    
             response = requests.get(url=new_url,headers=headers)
    
             #解析response中的图片链接
    
             e = '<div class="thumb">.*?<img src="(.*?)".*?>.*?</div>'
    
             pa = re.compile(e,re.S)
    
             image_urls = pa.findall(response.text)
    
              #循环下载该页码下所有的图片数据
    
             for image_url in image_urls:
    
                 image_url = 'https:' + image_url
    
                 image_name = image_url.split('/')[-1]
    
                 image_path = 'images/'+image_name
    
                 image_data = requests.get(url=image_url,headers=headers).content
    
                 with open(image_path,'wb') as fp:
    
                     fp.write(image_data)
    

    项目需求:爬取糗事页面糗图,并以alt命名存图片(me)

    import requests
    import re
    import os
    agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
    headers = {
        'User-Agent': agent,
    }
    proxy={
        "https":"121.8.98.196:80",
    }
    # 单页爬取
    url = 'https://www.qiushibaike.com/pic/'
    page_text = requests.get(url=url,headers=headers).text
    img_url = re.findall('<div class="thumb">.*?<img src="(.*?)" alt="(.*?)" />.*?</div>',page_text,re.S) # 获取img的src和alt属性的值,tuple中存
    # 测试获取图片alt命名代码片段
    # for img_u1 in img_url:
    #     print(img_u1[0]+'/'+img_u1[1])
    #     print('.'+img_u1[0].split('.')[-1])
    #     img_name = img_u1[1]
    #     img_end = '.'+img_u1[0].split('.')[-1]
    #     img_name = img_name + img_end
    #     print(img_name)
    
    if not os.path.exists('./img_qiubai'):
        os.mkdir('img_qiubai')
    for img_u in img_url:
        img_u2 = 'https:' + img_u[0]
        print(img_u2)
        img_end = '.'+img_u[0].split('.')[-1]
        img_text = requests.get(url=img_u2,headers=headers).content
    #     img_name = img_u.split('/')[-1] #  img路径的最后字符串
        img_name = img_u[1] # img的alt值
        img_path = './img_qiubai/' + img_name + img_end
        with open(img_path,'wb') as fp:
            fp.write(img_text)
            print('%s....done..............'%img_name)
        
    # # 按照输入页码爬取
    # start_page = int(input('start page:'))
    # end_page = int(input('end page:'))
    # for num in range(start_page,end_page + 1):
        
    #     url = 'https://www.qiushibaike.com/pic/page/%s/'%num
    #     page_text = requests.get(url=url,headers=headers).text
    #     img_url = re.findall('<div class="thumb">.*?<img src="(.*?)".*?>.*?</div>',page_text,re.S) # 获取img的src的数据值
    #     print(img_url)
    #     if not os.path.exists('./img_qiubai1'):
    #         os.mkdir('img_qiubai1')
    #     for img_u in img_url:
    #         img_u = 'https:' + img_u
    #         print(img_u)
    #         img_text = requests.get(url=img_u,headers=headers).content
    #         img_name = img_u.split('/')[-1]
    #         img_path = './img_qiubai1/' + img_name
    #         with open(img_path,'wb') as fp:
    #             fp.write(img_text)
    #             print('%s....done..............'%img_name)
    

    二.Xpath解析

    xpath插件安装:

    - xpath插件:就可以直接将xpath表达式作用于浏览器的网页当中
    - 安装:更多工具-》扩展程序-》开启右上角的开发者模式-》xpath插件拖动到页面即可(xpath.crx)
    - 快捷键:
        开启和关闭xpath插件:ctrl + shift + x

    测试页面数据

    <html lang="en">
    <head>
    
    	<meta charset="UTF-8" />
    
    	<title>测试bs4</title>
    </head>
    <body>
    	<div>
    		<p>百里守约</p>
    	</div>
    	<div class="song">
    
    		<p>李清照</p>
    
    		<p>王安石</p>
    
    		<p>苏轼</p>
    
    		<p>柳宗元</p>
    
    		<a href="http://www.song.com/" title="赵匡胤" target="_self">
    
    			<span>this is span</span>
    
    		宋朝是最强大的王朝,不是军队的强大,而是经济很强大,国民都很有钱</a>
    
    		<a href="" class="du">总为浮云能蔽日,长安不见使人愁</a>
    
    		<img src="http://www.baidu.com/meinv.jpg" alt="" />
    	</div>
    	<div class="tang">
    		<ul>
    			<li><a href="http://www.baidu.com" title="qing">清明时节雨纷纷,路上行人欲断魂,借问酒家何处有,牧童遥指杏花村</a></li>
    
    			<li><a href="http://www.163.com" title="qin">秦时明月汉时关,万里长征人未还,但使龙城飞将在,不教胡马度阴山</a></li>
    
    			<li><a href="http://www.126.com" alt="qi">岐王宅里寻常见,崔九堂前几度闻,正是江南好风景,落花时节又逢君</a></li>
    
    			<li><a href="http://www.sina.com" class="du">杜甫</a></li>
    
    			<li><a href="http://www.dudu.com" class="du">杜牧</a></li>
    
    			<li><b>杜小月</b></li>
    
    			<li><i>度蜜月</i></li>
    
    			<li><a href="http://www.haha.com" id="feng">凤凰台上凤凰游,凤去台空江自流,吴宫花草埋幽径,晋代衣冠成古丘</a></li>
    
    		</ul>
    	</div>
    </body>
    </html>
    

    常用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表达式进行数据解析:

    1.下载:pip install lxml
    
    2.导包:from lxml import etree
    
    3.将html文档或者xml文档转换成一个etree对象,然后调用对象中的方法查找指定的节点
    
      2.1 本地文件:tree = etree.parse(文件名)
    
                    tree.xpath("xpath表达式")
    
      2.2 网络数据:tree = etree.HTML(网页内容字符串)
    
                    tree.xpath("xpath表达式")
    

     安装xpath插件在浏览器中对xpath表达式进行验证:可以在插件中直接执行xpath表达式

    • 将xpath插件拖动到谷歌浏览器拓展程序(更多工具)中,安装成功

    • 启动和关闭插件 ctrl + shift + x

    项目需求:获取好段子中段子的内容和作者   http://www.haoduanzi.com

    from lxml import etree
    
    import requests
    
    
    
    url='http://www.haoduanzi.com/category-10_2.html'
    
    headers = {
    
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
    
        }
    
    url_content=requests.get(url,headers=headers).text
    
    #使用xpath对url_conten进行解析
    
    #使用xpath解析从网络上获取的数据
    
    tree=etree.HTML(url_content)
    
    #解析获取当页所有段子的标题
    
    title_list=tree.xpath('//div[@class="log cate10 auth1"]/h3/a/text()')
    
    ele_div_list=tree.xpath('//div[@class="log cate10 auth1"]')
    
    text_list=[] #最终会存储12个段子的文本内容
    
    for ele in ele_div_list:
    
        #段子的文本内容(是存放在list列表中)
    
        text_list=ele.xpath('./div[@class="cont"]//text()')
    
        #list列表中的文本内容全部提取到一个字符串中
    
        text_str=str(text_list)
    
        #字符串形式的文本内容防止到all_text列表中
    
        text_list.append(text_str)
    
    print(title_list)
    
    print(text_list)
    

    需求:搜狗搜索输入关键字,根据页码范围,保存结果的标题和内容简要于csv

    import requests
    import os
    from lxml import etree
    import csv
    # create dir
    
    def code_rev(str1):
        str11 = str1.encode('raw_unicode_escape')
        str12 = str11.decode()
        return str12
    
    def get_html(start_pageNum,end_pageNum,headers,word):    
        title_list = []  # 按照页码存放标题
        content_list = []
        
        for page in range(start_pageNum,end_pageNum + 1):
            params={
                'query':word,
                'page':page,
                'ie':'utf8',
            }
            response = requests.get(url=url,params=params,headers=headers)
            page_text = response.text
            #     print(page_text)
            
            tree=etree.HTML(page_text)
            
    
            div=tree.xpath('//div[@class="box-result"]/div[@class="result-about-list"]')
            title1_list = []
            content1_list = []
            fileName = 'pages/zhihu_python_%s.csv'%page
            with open(fileName, "w", newline="") as csvFile:
                for num in range(1,len(div)+1):
                    title1=tree.xpath('//div[@class="box-result"]/div[@class="result-about-list"][%s]/h4/a[1]//text()'%num)
                    title1_list.append(''.join(title1))
                    content1=tree.xpath('//div[@class="box-result"]/div[@class="result-about-list"][%s]/div/p[1]//text()'%num)
                    content1_list.append(''.join(content1))
                content_dict = dict(zip(title1_list,content1_list))
    
                print(content_dict)
            
                csvWriter = csv.writer(csvFile)
                for k,v in content_dict.items():
                    csvWriter.writerow([k,v])
                csvFile.close()
    if __name__ == '__main__':
    
        if not os.path.exists('./pages'):
            os.mkdir('./pages')
    
        url='https://zhihu.sogou.com/zhihu'
        agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
        headers = {
            'User-Agent': agent,
        }
        word = input("inter a word:")
        start_pageNum = int(input("enter a start pageNum:"))
        end_pageNum = int(input("enter a end pageNum:"))
        get_html(start_pageNum,end_pageNum,headers,word)
    
        # 写入文件,整页数据
    #     fileName = word+str(page)+'.html'
    #     filePath = 'pages/'+fileName
    #     with open(filePath,'w',encoding='utf-8') as fp:
    #         fp.write(page_text)
    #         print('%s 写入成功'%page)
    #  把标题和内容写入csv
        
    

    三.BeautifulSoup解析

    环境安装

    - 需要将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
    

    基础使用

    使用流程:       
    
        - 导包:from bs4 import BeautifulSoup
    
        - 使用方式:可以将一个html文档,转化为BeautifulSoup对象,然后通过对象的方法或者属性去查找指定的节点内容
    
            (1)转化本地文件:
    
                 - soup = BeautifulSoup(open('本地文件'), 'lxml')
    
            (2)转化网络文件:
    
                 - soup = BeautifulSoup('字符串类型或者字节类型', 'lxml')
    
            (3)打印soup对象显示内容为html文件中的内容
    
    
    
    基础巩固:
    
        (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选择器返回永远是列表,需要通过下标提取指定的对象
    

    需求:使用bs4实现将诗词名句网站中三国演义小说的每一章的内容爬去到本地磁盘进行存储   http://www.shicimingju.com/book/sanguoyanyi.html

    #!/usr/bin/env python
    
    # -*- coding:utf-8 -*-
    
    import requests
    
    from bs4 import BeautifulSoup
    
    headers={
    
             'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
    
         }
    
    def parse_content(url):
    
        #获取标题正文页数据
    
        page_text = requests.get(url,headers=headers).text
    
        soup = BeautifulSoup(page_text,'lxml')
    
        #解析获得标签
    
        ele = soup.find('div',class_='chapter_content')
    
        content = ele.text #获取标签中的数据值
    
        return content
    
    if __name__ == "__main__":
    
         url = 'http://www.shicimingju.com/book/sanguoyanyi.html'
    
         reponse = requests.get(url=url,headers=headers)
    
         page_text = reponse.text
    
         #创建soup对象
    
         soup = BeautifulSoup(page_text,'lxml')
    
         #解析数据
    
         a_eles = soup.select('.book-mulu > ul > li > a')
    
         print(a_eles)
    
         cap = 1
    
         for ele in a_eles:
    
             print('开始下载第%d章节'%cap)
    
             cap+=1
    
             title = ele.string
    
             content_url = 'http://www.shicimingju.com'+ele['href']
    
             content = parse_content(content_url)
    
    
    
             with open('./sanguo.txt','w') as fp:
    
                 fp.write(title+":"+content+'
    
    
    
    
    ')
    
                 print('结束下载第%d章节'%cap)
    
  • 相关阅读:
    Go语言实战_自己定义OrderedMap
    计算机图形学(二)输出图元_3_画线算法_2_DDA算法
    hibernate---java.lang.UnsupportedOperationException: The user must supply a JDBC connection
    OpenCV从入门到放弃(五):像素!
    ibatis 开发中的经验 (三)Struts+Spring+Ibatis 开发环境搭建
    HDU4911-Inversion
    hdu
    Maximum Likelihood Method最大似然法
    最小二乘法least square
    PASCAL VOC数据集The PASCAL Object Recognition Database Collection
  • 原文地址:https://www.cnblogs.com/fmgao-technology/p/10141331.html
Copyright © 2020-2023  润新知