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

mat.py文件

 # -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractor import LinkExtractor
from weidashang.items import matplotlib class MatSpider(scrapy.Spider):
name = "mat"
allowed_domains = ["matplotlib.org"]
start_urls = ['https://matplotlib.org/examples'] def parse(self, response):
       #抓取每个脚本文件的访问页面,拿到后下载
link = LinkExtractor(restrict_css='div.toctree-wrapper.compound li.toctree-l2')
for link in link.extract_links(response):
yield scrapy.Request(url=link.url,callback=self.example) def example(self,response):
      #进入每个脚本的页面,抓取源码文件按钮,并和base_url结合起来形成一个完整的url
href = response.css('a.reference.external::attr(href)').extract_first()
url = response.urljoin(href)
example = matplotlib()
example['file_urls'] = [url]
return example

pipelines.py

 class MyFilePlipeline(FilesPipeline):
def file_path(self, request, response=None, info=None):
path = urlparse(request.url).path
return join(basename(dirname(path)),basename(path))

settings.py

 ITEM_PIPELINES = {
'weidashang.pipelines.MyFilePlipeline': 1,
}
FILES_STORE = 'examples_src'

items.py

class matplotlib(Item):
file_urls = Field()
files = Field()

run.py

 from scrapy.cmdline import execute
execute(['scrapy', 'crawl', 'mat','-o','example.json'])

python爬虫之scrapy文件下载的更多相关文章

  1. 教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神

    本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http://www.xiaohuar.com/,让你体验爬取校花的成就感. Scr ...

  2. 【转载】教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神

    原文:教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神 本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http:/ ...

  3. Linux 安装python爬虫框架 scrapy

    Linux 安装python爬虫框架 scrapy http://scrapy.org/ Scrapy是python最好用的一个爬虫框架.要求: python2.7.x. 1. Ubuntu14.04 ...

  4. Python爬虫框架Scrapy实例(三)数据存储到MongoDB

    Python爬虫框架Scrapy实例(三)数据存储到MongoDB任务目标:爬取豆瓣电影top250,将数据存储到MongoDB中. items.py文件复制代码# -*- coding: utf-8 ...

  5. 《Python3网络爬虫开发实战》PDF+源代码+《精通Python爬虫框架Scrapy》中英文PDF源代码

    下载:https://pan.baidu.com/s/1oejHek3Vmu0ZYvp4w9ZLsw <Python 3网络爬虫开发实战>中文PDF+源代码 下载:https://pan. ...

  6. Python爬虫框架Scrapy教程(1)—入门

    最近实验室的项目中有一个需求是这样的,需要爬取若干个(数目不小)网站发布的文章元数据(标题.时间.正文等).问题是这些网站都很老旧和小众,当然也不可能遵守 Microdata 这类标准.这时候所有网页 ...

  7. 0.Python 爬虫之Scrapy入门实践指南(Scrapy基础知识)

    目录 0.0.Scrapy基础 0.1.Scrapy 框架图 0.2.Scrapy主要包括了以下组件: 0.3.Scrapy简单示例如下: 0.4.Scrapy运行流程如下: 0.5.还有什么? 0. ...

  8. 《精通Python爬虫框架Scrapy》学习资料

    <精通Python爬虫框架Scrapy>学习资料 百度网盘:https://pan.baidu.com/s/1ACOYulLLpp9J7Q7src2rVA

  9. 初识python爬虫框架Scrapy

    Scrapy,按照其官网(https://scrapy.org/)上的解释:一个开源和协作式的框架,用快速.简单.可扩展的方式从网站提取所需的数据. 我们一开始上手爬虫的时候,接触的是urllib.r ...

随机推荐

  1. Ubuntu开启或重启ssh服务

    开启ssh服务首先需要安装打开ssh服务的库: sudo apt-get install openssh-server 检查当前的ssh开启情况: ps -e |grep ssh 如果有sshd,则s ...

  2. redis的过期时间和过期删除机制

    一:设置过期时间 redis有四种命令可以用于设置键的生存时间和过期时间: EXPIRE <KEY> <TTL> : 将键的生存时间设为 ttl 秒 PEXPIRE <K ...

  3. UVA1374-Power Calculus(迭代加深搜索)

    Problem UVA1374-Power Calculus Accept:323  Submit:2083 Time Limit: 3000 mSec  Problem Description  I ...

  4. 基于SpringBoot的项目管理后台

    代码地址如下:http://www.demodashi.com/demo/13943.html 一.项目简介 在使用本项目之前,需要对SpringBoot,freemaker,layui,flyway ...

  5. [Vani有约会]雨天的尾巴

    嘟嘟嘟 看到链上操作,自然想到树剖. 先考虑序列上的问题:那么区间修改可以用差分.所以我们把操作拆成\(L\)和\(R + 1\)两个点,然后离线.排序后扫一遍,用线段树维护数量最多的颜色是哪一个. ...

  6. 转://Oracle not in查不到应有的结果(NULL、IN、EXISTS详解)

    问题: 语句1 : Select   *   from   table1 A  where  A.col1  not   in  (  select  col1  from  table2 B )  ...

  7. php面试中的经典问题

    原文:https://blog.csdn.net/ghostlv/article/details/51284745 问题一问题描述考虑下面代码: $str1 = 'yabadabadoo';$str2 ...

  8. redis单例模式写法

    <?php /**只看红色重点 * =========================================================== * ZW_Memory_Cache * ...

  9. python:while循环、运算符、初始编码

    while循环 while -- 关键字 while 条件: 缩进代码块 以上循环是(死循环) 终止循环的方法 1.break 跳出循环,并且把循环给干掉了 2.continue 跳出本次循环,继续下 ...

  10. handsontable-chosen-editor

    https://github.com/mydea/handsontable-chosen-editor handsontable-chosen-editor是handsontable column的扩 ...