我们在写普通脚本的时候,从一个网站拿到一个文件的下载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. C# 编写windows服务及服务的安装、启动、删除、定时执行任务

    一.编写windows服务 1.VS2017  - 创建服务Myservice 2.创建好项目之后 --- >> 双击 Service1.cs  ---- >>  出现一个设计 ...

  2. ORA-4031 错误故障排除与诊断[视频] (Doc ID 2016002.1)

    Copyright (c) 2019, Oracle. All rights reserved. Oracle Confidential.     ORA-4031 错误故障排除与诊断[视频] (Do ...

  3. HTTP协议、HTTP请求方法、常见状态码、HTTP消息

    HTTP协议 客户端请求,服务端响应.浏览器与服务器不建立持久连接,响应后连接失效. HTTP请求方法 一.GET GET方法用于获取请求页面的指定信息. 二.HEAD 除了服务器不能在响应里返回消息 ...

  4. PAT A1055 The World's Richest (25 分)——排序

    Forbes magazine publishes every year its list of billionaires based on the annual ranking of the wor ...

  5. python:unittest之discover()方法批量执行用例

    自动化测试过程中,自动化覆盖的功能点和对应测试用例之间的关系基本都是1 VS N,如果每次将测试用例一个个单独执行,不仅效率很低, 无法快速反馈测试结果,而且维护起来很麻烦.在python的单元测试框 ...

  6. Apache Commons Codec的Base64加解密库

    下载地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi import org.apache.commons.cod ...

  7. 封装HttpUrlConnection开箱即用

    因为经常用到 便写出来方边使用 直接复制本类即可 import java.io.*; import java.net.HttpURLConnection; import java.net.URL; i ...

  8. LeetCode 832. Flipping an Image

    Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...

  9. configure: error: cannot guess build type; you must specify one解决方法

    原文地址:https://blog.csdn.net/hebbely/article/details/53993141 1.configure: error: cannot guess build t ...

  10. H5 文字属性

    03-文字属性 我是文字 我是文字 abc我是段落 <!DOCTYPE html> <html lang="en"> <head> <me ...