scrapy--matplotlib
昨天晚上看了一些关于保存文件的相关资料,早早的睡了,白天根据网上查找的资料,自己再捡起来.弄了一上午就爬取出来了,开心!!!好吧,让我们开始
老规矩,先上图。大家也赶快行动起来
分类文件:

文件内coding.py

1.matlib.py
# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from Matlib.items import MatlibItem
import pdb class MatlibSpider(scrapy.Spider):
name = 'matlib'
allowed_domains = ['matplotlib.org']
start_urls = ['https://matplotlib.org/examples/index.html'] def parse(self, response):
#le = LinkExtractor(restrict_css='div.toctree-wrapper.compound li.toctree-l2')
le = LinkExtractor(restrict_css='div.toctree-wrapper.compound li.toctree-l1', deny='/index.html$')
for link in le.extract_links(response):
yield scrapy.Request(link.url,callback=self.parse_url) def parse_url(self,response):
sel = response.css('a.reference.external::attr(href)').extract()[0]
url = response.urljoin(sel)
mpl = MatlibItem()
#mpl['files_url'] = [url]
#pdb.set_trace()
mpl['files_url'] = url.encode('utf-8')
#return mpl
yield mpl
2.items.py
import scrapy
class MatlibItem(scrapy.Item):
files_url = scrapy.Field()
files = scrapy.Field()
3.pipelines.py# -*- coding: utf-8 -*-
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html import scrapy
from scrapy.pipelines.files import FilesPipeline
import urlparse
from os.path import basename,dirname,join
import pdb class MyFilePipeline(FilesPipeline): def get_media_requests(self,item,info): #调用这个方法,爬虫才能保存文件!!
#for file in item['files_url']:
#yield scrapy.Request(file.encode('utf-8'))
yield scrapy.Request(item['files_url'])
'''
def file_path(self, request, response=None, info=None): #重写文件名,和路径
split_url = str(request.url).split('/')
kind_name = split_url[-2]
file_name = split_url[-1]
return '%s/%s' % (kind_name, file_name)
'''
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(其他和我之前发表的一样,大家可以去查找下)
ITEM_PIPELINES = {
#'Matlib.pipelines.MatlibPipeline': 200,
'Matlib.pipelines.MyFilePipeline': 2,
#'scrapy.pipelines.files.FilesPipeline': 1,
}
遇到的问题:
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的更多相关文章
- scrapy实例matplotlib脚本下载
利用scrapy框架实现matplotlib实例脚本批量下载至本地并进行文件夹分类:话不多说上代码: 首先是爬虫代码: import scrapy from scrapy.linkextractors ...
- win7下matplotlib安装(64位)
前段时间爬了一些数据,想着以后要将数据的分析结果什么的展示出来,就想着下个MATLAB,某天在微信上的一篇文章发现matplotlib库,是用于Python的一个不错的图形化库,就想着装上耍耍.不过安 ...
- Python爬虫入门教程 37-100 云沃客项目外包网数据爬虫 scrapy
爬前叨叨 2019年开始了,今年计划写一整年的博客呢~,第一篇博客写一下 一个外包网站的爬虫,万一你从这个外包网站弄点外快呢,呵呵哒 数据分析 官方网址为 https://www.clouderwor ...
- Python爬虫入门教程 38-100 教育部高校名单数据爬虫 scrapy
爬前叨叨 今天要爬取一下正规大学名单,这些名单是教育部公布具有招生资格的高校名单,除了这些学校以外,其他招生的单位,其所招学生的学籍.发放的毕业证书国家均不予承认,也就是俗称的野鸡大学! 网址是 ht ...
- Scrapy爬取猫眼《复仇者联盟4终局之战》影评
一.分析 首先简单介绍一下Scrapy的基本流程: 引擎从调度器中取出一个链接(URL)用于接下来的抓取 引擎把URL封装成一个请求(Request)传给下载器 下载器把资源下载下来,并封装成应答包( ...
- python爬虫之scrapy文件下载
我们在写普通脚本的时候,从一个网站拿到一个文件的下载url,然后下载,直接将数据写入文件或者保存下来,但是这个需要我们自己一点一点的写出来,而且反复利用率并不高,为了不重复造轮子,scrapy提供很流 ...
- scrapy爬取知乎问答
登陆 参考 https://github.com/zkqiang/Zhihu-Login # -*- coding: utf-8 -*- import scrapy import time impor ...
- scrapy 登陆知乎
参考 https://github.com/zkqiang/Zhihu-Login # -*- coding: utf-8 -*- import scrapy import time import r ...
- PyCharm+Scrapy爬取安居客楼盘信息
一.说明 1.1 开发环境说明 开发环境--PyCharm 爬虫框架--Scrapy 开发语言--Python 3.6 安装第三方库--Scrapy.pymysql.matplotlib 数据库--M ...
- 使用scrapy爬取dota2贴吧数据并进行分析
一直好奇贴吧里的小伙伴们在过去的时间里说的最多的词是什么,那我们就来抓取分析一下贴吧发文的标题内容,并提取分析一下,看看吧友们在说些什么. 首先我们使用scrapy对所有贴吧文章的标题进行抓取 scr ...
随机推荐
- jquery截取、判断字符串的长度,中英文都可
计算字符串的长度(一个双字节字符长度计2,ASCII字符计1) String.prototype.len=function(){return this.replace([^\x00-\xff]/g,& ...
- 参数化拼接in查询条件,个人备份
/// <summary> /// 查询数据报表 /// </summary> /// <param name="Tr ...
- 浅谈MVC基础
ASP.NET MVC :UI层框架 让我们的web开发又回到了本质:请求,处理,响应 MVC本身是一种思想,将程序分成三个模块 Model:模型 广义的说法(包含DAL BLL MODEL ...
- 日期API
Java 8 在包java.time下包含了一组全新的时间日期API.下面的例子展示了这组新API里最重要的一些部分: 1.Clock 时钟 Clock类提供了访问当前日期和时间的方法,Clock是时 ...
- guava的重试机制guava-retrying使用
1,添加maven依赖 <dependency> <groupId>com.github.rholder</groupId> <artifactId>g ...
- centreon公司推出的check plugin pack
文档 http://documentation.centreon.com/docs/centreon-plugins/en/latest/ (epel) # yum install nagios-pl ...
- php读取mysql中文乱码
连接mysql的文件: <?php /***************************** *数据库连接 *****************************/ $conn = @m ...
- C++ POD类型
POD( Plain Old Data)概念: Arithmetic types (3.9.1), enumeration types, pointer types, and pointer to m ...
- Gameplay Classes
每个虚幻游戏类都是一个.h和一个.cpp组成. 类在虚幻中有便准的命名模式. 前缀: A继承于可量产的游戏性类.他们都是Actor,可以直接在游戏中生成. U继承于所有游戏性对象.不能在游戏中直接生成 ...
- CRUD全栈式编程架构之界面层的设计
Layout的设计 模板模式 mvc的模板特别类似设计模式中模板方法模式,结合Layout中RenderSection和RenderBody方法可以将部分html展现逻辑延迟到具体的视图页面去实现里面 ...