zoukankan      html  css  js  c++  java
  • scrapy 中用selector来提取数据的用法

     

    一. 基本概念

    1. Selector是一个可独立使用的模块,我们可以用Selector类来构建一个选择器对象,然后调用它的相关方法如xpaht(), css()等来提取数据,如下
    from  scrapy import Selector
    body= '<html><head><title>Hello World</title></head><body></body> </ html> ’
    selector  = Selector(text=body)
    title  = selector.xpath('//title/text()').extract_first()
    print(title)
    
    
    
    输出为
    Hello World
     
     
    2. scrapy shell 主要用于测试scrapy项目中命令是否生效,可在bash下直接执行,
    这里我们通过使用scrapy shell来验证学习选择器提取网页数据,在linux中bash下执行命令
    scrapy shell http://doc.scrapy.org/en/latest/_static/selectors-sample1.html即可进入scrapy shell命令模式
    上面测试网站源码
    <html>
     <head>
      <base href='http://example.com/' />
      <title>Example website</title>
     </head>
     <body>
      <div id='images'>
       <a href='image1.html'>Name: My image 1 <br /><img src='image1_thumb.jpg' /></a>
       <a href='image2.html'>Name: My image 2 <br /><img src='image2_thumb.jpg' /></a>
       <a href='image3.html'>Name: My image 3 <br /><img src='image3_thumb.jpg' /></a>
       <a href='image4.html'>Name: My image 4 <br /><img src='image4_thumb.jpg' /></a>
       <a href='image5.html'>Name: My image 5 <br /><img src='image5_thumb.jpg' /></a>
      </div>
     </body>
    </html>
     

     

    二.  scrapy shell中有内置选择器response.selector,可用于提取网页信息,几个例子如下

    1. xpath和css的基本用法

    #获取<title>的文本值,其中第一个selector字符可以不写
    response.selector.xpath('//title/text()').extract_first()response.selector.css('title::text').extract_first()
    
    
    #获取a标签的href属性值
    response.xpath('//a/@href').extract()
    response.css('a::attr(href)').extract() 
    
    
    #查找属性名称包含image字样的所有a标签
     response.xpath('//a[contains(@href, "image")]/@href').extract()
     response.css('a[href*=image]::attr(href)').extract()
    
    
    #查找属性名称包含image字样的所有a标签,并且在下级img目录下的src属性值
     response.xpath('//a[contains(@href, "image")]/img/@src').extract()
     response.css('a[href*=image] img::attr(src)').extract()
    
    
    #结合正则表达式提取所需内容
     response.css('a::text').re('Name:(.*)')   #提取(.*)代表的内容
     response.css('a::text').re_first('Name:(.*)').strip()  #提取第一个(.*)代表的内容,strip()去除首尾空格

     

    2. xpath和css也可以一起用

    #先选上src属性标签
    response.xpath('//div[@id="images"]').css('img::attr(src)'))
    #提取相应信息
    response.xpath('//div[@id="images"]').css('img::attr(src)')).extract() #得到多个字符值
    response.xpath('//div[@id="images"]').css('img::attr(src)')).extract_first() #得到一个字符值
    response.xpath('//div[@id="images"]').css('img::attr(src)')).extract_first(default='') #如果没提取到返回默认值
     
    
    注意:
    1. extract()方法把selector类型变为数据类型
    2. [@id="images"]表示用属性来限制匹配的范围,只查找id属性值等于images的div标签,经测试[]中的id属性值image必须用双引号
    
    
  • 相关阅读:
    笨办法学习python之hashmap
    python实现三级菜单源代码
    ql的python学习之路-day3
    ql的python学习之路-day2
    Spring的数据库开发
    Spring学习之Aspectj开发实现AOP
    spring学习之依赖注入DI与控制反转IOC
    spring学习之第一个spring程序
    spring学习之spring入门
    Java线程(一)——创建线程的两种方法
  • 原文地址:https://www.cnblogs.com/regit/p/9402626.html
Copyright © 2011-2022 走看看