昨天晚上看了一些关于保存文件的相关资料,早早的睡了,白天根据网上查找的资料,自己再捡起来.弄了一上午就爬取出来了,开心!!!好吧,让我们开始

老规矩,先上图。大家也赶快行动起来

分类文件:

文件内coding.py

1.matlib.py

  1. # -*- coding: utf-8 -*-
  2. import scrapy
  3. from scrapy.linkextractors import LinkExtractor
  4. from Matlib.items import MatlibItem
  5. import pdb
  6.  
  7. class MatlibSpider(scrapy.Spider):
  8. name = 'matlib'
  9. allowed_domains = ['matplotlib.org']
  10. start_urls = ['https://matplotlib.org/examples/index.html']
  11.  
  12. def parse(self, response):
  13. #le = LinkExtractor(restrict_css='div.toctree-wrapper.compound li.toctree-l2')
  14. le = LinkExtractor(restrict_css='div.toctree-wrapper.compound li.toctree-l1', deny='/index.html$')
  15. for link in le.extract_links(response):
  16. yield scrapy.Request(link.url,callback=self.parse_url)
  17.  
  18. def parse_url(self,response):
  19. sel = response.css('a.reference.external::attr(href)').extract()[0]
  20. url = response.urljoin(sel)
  21. mpl = MatlibItem()
  22. #mpl['files_url'] = [url]
  23. #pdb.set_trace()
  24. mpl['files_url'] = url.encode('utf-8')
  25. #return mpl
  26. yield mpl

2.items.py

  1. import scrapy
  2.  
  3. class MatlibItem(scrapy.Item):
  4.  
  5. files_url = scrapy.Field()
  6. files = scrapy.Field()

3.pipelines.py# -*- coding: utf-8 -*-


  1. # Define your item pipelines here
  2. #
  3. # Don't forget to add your pipeline to the ITEM_PIPELINES setting
  4. # See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
  5. import scrapy
  6. from scrapy.pipelines.files import FilesPipeline
  7. import urlparse
  8. from os.path import basename,dirname,join
  9. import pdb
  10. class MyFilePipeline(FilesPipeline):
  11. def get_media_requests(self,item,info): #调用这个方法,爬虫才能保存文件!!
  12. #for file in item['files_url']:
  13. #yield scrapy.Request(file.encode('utf-8'))
  14. yield scrapy.Request(item['files_url'])
  15.   
  16. '''
  17. def file_path(self, request, response=None, info=None): #重写文件名,和路径
  18. split_url = str(request.url).split('/')
  19. kind_name = split_url[-2]
  20. file_name = split_url[-1]
  21. return '%s/%s' % (kind_name, file_name)
  22. '''
    def file_path(self,request,response=None,info=None):
    path=urlparse.urlparse(request.url).path
         return join(basename(dirname(path)),basename(path))

4.settings.py(其他和我之前发表的一样,大家可以去查找下)

  1. ITEM_PIPELINES = {
  2. #'Matlib.pipelines.MatlibPipeline': 200,
  3. 'Matlib.pipelines.MyFilePipeline': 2,
  4. #'scrapy.pipelines.files.FilesPipeline': 1,
  5. }

遇到的问题:

  1.matlib.py

    1.url = response.urljoin(sel) :url需要解码才能在pipelines.py/scrapy.Request(item[''files_url]) 中运行下载

  2.pipelines.py:

    1.未重写文件名,会自动保存为checksum名,

      'checksum': '715610c4375a1d749bc26b39cf7e7199',
      'path': 'animation/bayes_update.py',
      'url': 'https://matplotlib.org/examples/animation/bayes_update.py'}],

    2.def get_media_requests(self,item,info): 调用这个函数,文件才能下载!!!看其他人重写路径的时候,没有调用这个函数,误导了我很久

如果有小伙伴,遇到了其他问题,欢迎留言,大家一起进步

scrapy--matplotlib的更多相关文章

  1. scrapy实例matplotlib脚本下载

    利用scrapy框架实现matplotlib实例脚本批量下载至本地并进行文件夹分类:话不多说上代码: 首先是爬虫代码: import scrapy from scrapy.linkextractors ...

  2. win7下matplotlib安装(64位)

    前段时间爬了一些数据,想着以后要将数据的分析结果什么的展示出来,就想着下个MATLAB,某天在微信上的一篇文章发现matplotlib库,是用于Python的一个不错的图形化库,就想着装上耍耍.不过安 ...

  3. Python爬虫入门教程 37-100 云沃客项目外包网数据爬虫 scrapy

    爬前叨叨 2019年开始了,今年计划写一整年的博客呢~,第一篇博客写一下 一个外包网站的爬虫,万一你从这个外包网站弄点外快呢,呵呵哒 数据分析 官方网址为 https://www.clouderwor ...

  4. Python爬虫入门教程 38-100 教育部高校名单数据爬虫 scrapy

    爬前叨叨 今天要爬取一下正规大学名单,这些名单是教育部公布具有招生资格的高校名单,除了这些学校以外,其他招生的单位,其所招学生的学籍.发放的毕业证书国家均不予承认,也就是俗称的野鸡大学! 网址是 ht ...

  5. Scrapy爬取猫眼《复仇者联盟4终局之战》影评

    一.分析 首先简单介绍一下Scrapy的基本流程: 引擎从调度器中取出一个链接(URL)用于接下来的抓取 引擎把URL封装成一个请求(Request)传给下载器 下载器把资源下载下来,并封装成应答包( ...

  6. python爬虫之scrapy文件下载

    我们在写普通脚本的时候,从一个网站拿到一个文件的下载url,然后下载,直接将数据写入文件或者保存下来,但是这个需要我们自己一点一点的写出来,而且反复利用率并不高,为了不重复造轮子,scrapy提供很流 ...

  7. scrapy爬取知乎问答

    登陆 参考 https://github.com/zkqiang/Zhihu-Login # -*- coding: utf-8 -*- import scrapy import time impor ...

  8. scrapy 登陆知乎

    参考 https://github.com/zkqiang/Zhihu-Login # -*- coding: utf-8 -*- import scrapy import time import r ...

  9. PyCharm+Scrapy爬取安居客楼盘信息

    一.说明 1.1 开发环境说明 开发环境--PyCharm 爬虫框架--Scrapy 开发语言--Python 3.6 安装第三方库--Scrapy.pymysql.matplotlib 数据库--M ...

  10. 使用scrapy爬取dota2贴吧数据并进行分析

    一直好奇贴吧里的小伙伴们在过去的时间里说的最多的词是什么,那我们就来抓取分析一下贴吧发文的标题内容,并提取分析一下,看看吧友们在说些什么. 首先我们使用scrapy对所有贴吧文章的标题进行抓取 scr ...

随机推荐

  1. mysql+C#

    MySqlParameter[] prams = ]; prams[] = new MySqlParameter("@personindex", personindex); pra ...

  2. 【踩坑】报错 non-static method xxx() cannot be referenced from a static context

    今天测试代码时遇到 Error:(6, 55) java: non-static method sayGoodbye() cannot be referenced from a static cont ...

  3. css的三个特性 背景透明设置

    关于行内元素(补充一点) 行内元素只能容纳文本或其他行内元素.(a特殊a里面可以放块级元素) 例子: 关于行高tip: 选择器的嵌套层级不应大于3级,位置靠后的限定条件应尽可能的精确. 属性定义必须另 ...

  4. (生产)create-keyframe-animation -动画实现

    参考:https://github.com/HenrikJoreteg/create-keyframe-animation 实例 var animations = require('create-ke ...

  5. vue组件双向绑定.sync修饰符的一个坑

    我们知道组件是单项的,但是有时候需要双向,这时候我们可以使用.sync修饰符,但今天遇到一个坑,一直不成功,花了半小时试出来的.... 在编程的时候我们很习惯冒号后面跟着空格.而.sync双向绑定需要 ...

  6. jQ中css()和addClass的区别之优先级

    笔者在实现点击表格中某行添加高亮的效果时,发现一个css()和addClassd()的冲突,具体代码如下: <style> .se { background:#FF6500; color: ...

  7. Miner3D Basic基础版

    ——强大的数据可视化软件 数据分析并不很复杂,Miner3D Basic基础版首先使用简单的方法,创造了强劲的图形驱动的数据处理模型,然后通过一个完整的视图为基本的交互环境,对基本数据进行分析,并通过 ...

  8. 【MATLAB】画信号频谱的子函数

    输入信号序列和采样率,该子函数可以画出该信号的频谱图. function [f,spec,NFFT]=spec_fft_plot(sample,L,Fs) % 输入数据说明: % sample:信号序 ...

  9. Ubuntu 14.04 软件源服务器集合

    http://wiki.ubuntu.com.cn/Template:14.04source 服务器列表 可将 http://cn.archive.ubuntu.com/ubuntu/ 替换为下列任意 ...

  10. sqlalchemy使用tip

    https://docs.sqlalchemy.org/en/latest/orm/tutorial.html http://docs.sqlalchemy.org/en/latest/core/sq ...