Scrapy 1.3.0 使用简介

scrapy 1.3.0  python 2.7

创建一个项目:

Before you startscraping, you will have to set up a new Scrapy project. Enter a directory whereyou’d like to store your code and run:

scrapy startproject tutorial

然后就会得到一系列文件:

第一个爬虫

import scrapy

class QuotesSpider(scrapy.Spider):

    name ="quotes"

    def start_requests(self):

        urls = [

            'Quotes to Scrape',

            'Quotes to Scrape',

        ]

        for url in urls:

            yield scrapy.Request(url=url,callback=self.parse)

    def parse(self,response):

        page =response.url.split("/")[-2]

        filename = 'quotes-%s.html' % page

        with open(filename, 'wb') as f:

            f.write(response.body)

        self.log('Saved file %s' % filename)

自定义的QuotesSpider类继承了scrapy.Spider类,并且有三个属性:

  1. name:用来识别爬虫,必须唯一
  2. start_requests():必须返回一个请求连接的可迭代的对象(一个请求的生成器或者列表)
  3. parse():被调用,用来处理服务器的响应,response 参数是TextResponse 的实例,保存整个网页用来被更有用的函数处理。

运行爬虫:

scrapy crawl quotes

结果:

刚刚的运行过程:

start_requests方法返回了scrapy的请求清单(scrapy.Request objects),

一旦接收到请求,scrapy会初始化Response对象,并且调用相关方法(例子中用的是parse方法)

将response传递给它。

 start_requests简介:

用urls生成请求列表的start_requests()方法,可以用写了一系列的URLS的start_urls属性代替,

这个列表将会被默认的接口实现start_requests(),来初始化spider的请求。

import scrapy

class QuotesSpider(scrapy.Spider):

    name = "quotes"

    start_urls =[

        'Quotes to Scrape',

        'Quotes to Scrape',

    ]

    def parse(self, response):

        page =response.url.split("/")[-2]

        filename = 'quotes-%s.html' % page

        with open(filename, 'wb') as f:

            f.write(response.body)

parse函数没有显式调用,因为在scrapy中parse是默认的回调方法

抽取数据

scrapy最好用的学习抽取数据的方法是选择器来使用scrapy shell。

Scrapy shell — Scrapy 2.11.0 documentation

Scrapy shell会自动用下载的网页创建一些实用对象,例如:

Response object andthe Selector objects (for both HTML and XML content)

使用scrapy shell测试数据

当抽取数据为空时,可以用浏览器查看请求的网页

Finally you hitCtrl-D (or Ctrl-Z in Windows) to exit the shell and resume the crawling:

用css选择器来抽取数据

scrapy shell "Quotes to Scrape"

使用 response.css('title')抽取数据会得到一张叫“ SelectorList”的列表。SelectorList代表 Selector对象列表,这个对象包装了 XML/HTML的元素,这些元素可以因一部的抽取数据。

 ::text 用在CSS查询中, 表示我们只想抽取 <title> 标签中的text元素。

因为extract只是获取到一个列表,所以有extract_first()、response.css('title::text')[0].extract()这样的用法,可以直接抽取到列表中的元素

注意: using.extract_first() avoids an IndexError andreturns None when it doesn’t find any element matching the selection.

参考下载的页面学习:

后面是使用正则表达式抽取数据

XPath:a brief intro

除了CSS, Scrapy 选择器也支持 XPath的表达形式:

使用火狐浏览器的firebug:

抽取名言和作者

首先观察网页 Quotes to Scrape

抽取特定内容:

空格好像是用来处理div class=“tags”这个 div标签中第一个标签。

知道每个数据怎么取出后,可以使用代码获得:

for quote inresponse.css("div.quote"):

...     text =quote.css("span.text::text").extract_first()

...     author =quote.css("small.author::text").extract_first()

...     tags = quote.css("div.tagsa.tag::text").extract()

...     print(dict(text=text, author=author,tags=tags))

最后得到的爬虫:

import scrapy

class QuotesSpider(scrapy.Spider):

   name = "quotes"

   start_urls = [

        'Quotes to Scrape',

        'Quotes to Scrape',

   ]

   def parse(self, response):

        for quote in response.css('div.quote'):

            yield {

                'text':quote.css('span.text::text').extract_first(),

                'author': quote.css('spansmall::text').extract_first(),

                'tags': quote.css('div.tagsa.tag::text').extract(),

            }

存储爬取的数据:

使用命令行:

  1. scrapy crawl quotes -o quotes.json  -json格式
  2. scrapy crawl quotes -o quotes.jl  -jsonlines格式

下一页:

先观察代码:

但是这样只能获取锚元素,想要获得连接可以:

下面是能自动进入下一页爬取的爬虫:

import scrapy

class QuotesSpider(scrapy.Spider):

   name = "quotes"

   start_urls = [

        'Quotes to Scrape',

   ]

   def parse(self, response):

        for quote in response.css('div.quote'):

            yield {

                'text':quote.css('span.text::text').extract_first(),

                'author': quote.css('spansmall::text').extract_first(),

                'tags': quote.css('div.tagsa.tag::text').extract(),

            }

        next_page = response.css('li.nexta::attr(href)').extract_first()

        if next_page is not None:

            next_page =response.urljoin(next_page)

            yield scrapy.Request(next_page,callback=self.parse)

至此爬虫可以用urljoin()建立一个绝对URL,并且能产生到下一页的新请求,然后将

自己注册到毁掉函数中,抽取下一页数据,直到爬完所有数据。

通过以上方法,可以构建一个复杂的爬虫,按照用户定义rules来爬取网页。

使用scrapy参数:

import scrapy

class QuotesSpider(scrapy.Spider):

   name = "quotes"

   def start_requests(self):

        url = 'Quotes to Scrape'

       tag = getattr(self, 'tag', None)

       if tag is not None:

            url = url + 'tag/' + tag

        yield scrapy.Request(url, self.parse)

   def parse(self, response):

        for quote in response.css('div.quote'):

            yield {

                'text':quote.css('span.text::text').extract_first(),

                'author': quote.css('span smalla::text').extract_first(),

            }

        next_page = response.css('li.nexta::attr(href)').extract_first()

        if next_page is not None:

            next_page =response.urljoin(next_page)

            yield scrapy.Request(next_page,self.parse)

针对上面的程序,使用命令:

scrapy crawl quotes -oquotes-humor.json -a tag=humor

 it will only visit URLs from the humor tag,such as  http://quotes.toscrape.com/tag/humor.

相关推荐

  1. Scrapy 1.3.0 使用简介

    2024-01-05 16:22:02       32 阅读
  2. Python库之Scrapy简介、安装、使用方法详细攻略

    2024-01-05 16:22:02       12 阅读
  3. 爬虫scrapy管道的使用

    2024-01-05 16:22:02       36 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-05 16:22:02       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-05 16:22:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-05 16:22:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-05 16:22:02       20 阅读

热门阅读

  1. [蓝桥杯 2016枚举]回文日期

    2024-01-05 16:22:02       34 阅读
  2. 谷歌地图搜索功能的bug

    2024-01-05 16:22:02       36 阅读
  3. hyper-V的虚拟磁盘扩容

    2024-01-05 16:22:02       34 阅读
  4. Typescript基础知识:函数类型和箭头函数

    2024-01-05 16:22:02       30 阅读
  5. chip-seq测序分析流程

    2024-01-05 16:22:02       41 阅读
  6. 【LeetCode】1164. 指定日期的产品价格

    2024-01-05 16:22:02       37 阅读
  7. mysql:SQL按时间查询方法总结

    2024-01-05 16:22:02       33 阅读
  8. centos7安装docker(包含yum配置阿里云镜像源)

    2024-01-05 16:22:02       37 阅读
  9. 解决.gitignore文件无效问题

    2024-01-05 16:22:02       39 阅读
  10. ffmpeg转码新技能

    2024-01-05 16:22:02       58 阅读
  11. Ajax同步调用影响加载动画展示问题

    2024-01-05 16:22:02       36 阅读
  12. anylabeling 加载模型后出错

    2024-01-05 16:22:02       50 阅读
  13. 偌依 项目部署及上线步骤

    2024-01-05 16:22:02       75 阅读
  14. 解析:Eureka的工作原理

    2024-01-05 16:22:02       36 阅读
  15. Eureka工作原理

    2024-01-05 16:22:02       34 阅读
  16. Eureka工作原理超详细讲解介绍

    2024-01-05 16:22:02       31 阅读