Python爬虫框架scrapy实现的文件下载功能示例
2018-08-04 08:56
1476 查看
我们在写普通脚本的时候,从一个网站拿到一个文件的下载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'] = Python Socket编程技巧总结" target=_blank> return example [/code]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相关内容可查看本站专题:《[url=https://www.jb51.net/Special/648.htm]Python Socket编程技巧总结
》、《[url=https://www.jb51.net/Special/667.htm]Python正则表达式用法总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》希望本文所述对大家Python程序设计有所帮助。
您可能感兴趣的文章:
相关文章推荐
- Python爬虫框架scrapy实现downloader_middleware设置proxy代理功能示例
- Python使用wget实现下载网络文件功能示例
- python爬虫框架scrapy实现模拟登录操作示例
- Scrapy:Python实现scrapy框架爬虫两个网址下载网页内容信息——Jason niu
- Python爬虫实现抓取京东店铺信息及下载图片功能示例
- Python视频爬虫实现下载头条视频功能示例
- [Python]网络爬虫(12):爬虫框架Scrapy的第一个爬虫示例入门教程
- [Python]网络爬虫(12):爬虫框架Scrapy的第一个爬虫示例入门教程
- python爬虫框架scrapy学习图片下载
- [Python]网络爬虫(12):爬虫框架Scrapy的第一个爬虫示例入门教程
- Python实现的下载网页源码功能示例
- Yii框架使用魔术方法实现跨文件调用功能示例
- python实现简单爬虫功能的示例
- [Python]网络爬虫(12):爬虫框架Scrapy的第一个爬虫示例入门教程
- Python实现爬虫爬取NBA数据功能示例
- Python的Flask框架及Nginx实现静态文件访问限制功能
- Servlet实现web站点文件下载功能示例
- [Python]网络爬虫(12):爬虫框架Scrapy的第一个爬虫示例入门教程
- [Python]网络爬虫(12):爬虫框架Scrapy的第一个爬虫示例入门教程
- [Python]网络爬虫(12):爬虫框架Scrapy的第一个爬虫示例入门教程