• 爬虫数据解析


    数据解析的目的是获取局部的数据

    数据解析的方法有正则,xpath,bs4

    正则:https://www.cnblogs.com/l1222514/p/11011009.html

    正则解析:
      import re
      #正则获取定位 可以获取括号里面的内容
      ex='xxxxx(.*?)xxxx'
      re.findall(ex,page_text,re.S)

    bs4解析
     解析原理:
      实例化一个Beautifulsoup的对象,且将页面源码数据加载到该对象中
      使用该对象的相关属性和方法实现标签定位和数据提取
    环境的安装:
      pip install bs4
      pip install lxml
    实例化Beautifulsoup对象
      BeautifulSoup(page_text,'lxml'):将从互联网上请求到的页面源码数据加载到该对象中
      BeautifulSoup(fp,'lxml'):将本地存储的一样页面源码数据加载到该对象中

    bs4的方法:
      from bs4 import BeautifulSoup
      soup = BeautifulSoup(fp,'lxml')
    方法1#soup.tagName:只可以定位到第一次出现的tagName标签
      soup.title
      soup.div
    方法2#soup.find(‘tagName’)
      soup.find('a') # ==soup.a
    方法3#属性定位
      soup.find('div',class_='song')
    方法4#find_all
      soup.find_all('div')[2]
    方法5#select('选择器') 选择器中class用.表示
      ‘>’:表示一个层级 空格:表示多个层级
      soup.select('.song')
      soup.select('.tang > ul > li > a') #==soup.select('.tang a')

    获取文本的方法##取文本:string取的是直系的文本数据,text获取的是全部的数据
      soup.p.string
      soup.find('div',class_='tang').get_text() #==soup.find('div',class_='tang').text
    获取属性的方法#取属性
      soup.a['href']
      soup.select('.tang > ul > li > a')[0]['href']

    xpath解析:
      解析效率比较高
      通用性最强的

    环境安装:pip install lxml
    解析原理:
      实例化一个etree对象且将即将被解析的页面源码数据加载到该对象中
      使用etree对象中的xpath方法结合着xpath表达式进行标签定位和数据提取
    实例化etree对象
      etree.parse('本地文件路径')
      etree.HTML(page_text)


    xpath定位方法:
      from lxml import etree
      tree = etree.parse('./test.html')
    方法1:#定位title标签
      #第一个/表示根目录
      tree.xpath('/html/head/title')
      tree.xpath('/html//title')
      tree.xpath('//title')
    方法2:#定位class=song的div
      tree.xpath('//div[@class="song"]')
      tree.xpath('//div[2]') #xpath表达式中的索引是从1开始
      tree.xpath('//div[@class="tang"]/ul/li[4]/a') #==tree.xpath('//div[@class="tang"]//li[4]/a')

    方法3:#取文本(获取李清照)
      /text() 获取直系文本 //text() 获取标签下的所有文本
      tree.xpath('//div[@class="song"]/p[1]/text()')[0]
      tree.xpath('//div[@class="song"]//text()')
    方法4:#取属性 /@属性

      tree.xpath('//a/@href')

    #爬取三国演义,使用xpath解析
    
    import requests
    from lxml import etree
    
    url = 'https://www.shicimingju.com/book/sanguoyanyi.html'
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'
    }
    #处理乱码问题
    respons  = requests.get(url = url,headers=headers)
    respons.encoding = 'utf-8'
    res_text = respons.text
    
    #实例化etree
    tree =etree.HTML(res_text)
    li_list = tree.xpath('//div[@class="book-mulu"]//li')
    fp = open('./sanguo','w',encoding='utf-8')
    for li in li_list:
        title = li.xpath('./a/text()')[0]
        detail_url = 'https://www.shicimingju.com/' +li.xpath('./a/@href')[0]
        #处理乱码问题
        datile_page = requests.get(url = detail_url,headers=headers)
        datile_page.encoding= 'utf-8'
        datile_page=datile_page.text
    
        tree = etree.HTML(datile_page)
        content = tree.xpath('//div[@class="chapter_content"]//text()')
        print(title,'下载完成')
        #持久化存储
        fp.write(title+'\n'+str(content) + '\n')
    
    fp.close()
    fp.flush()
    

      

  • 相关阅读:
    关于学习
    两个有序链表序列的合并
    谜题 UVA227
    周期串(Periodic Strings, UVa455)
    数数字(Digit Counting,ACM/ICPC Danang 2007,UVa1225)
    得分(Score, ACM/ICPC Seoul 2005,UVa 1585)
    201505061055_《Javascript编码规范笔记》
    201505031734_《JavaScript中的函数》
    201505030956_《Javascript变量整理》
    201505022013_《js好的坏的》
  • 原文地址:https://www.cnblogs.com/l1222514/p/16245832.html
Copyright © 2020-2023  润新知