scrapy爬虫抓取并下载文件

scrapy 内部提供了专门用于下载文件的 FilesPipeline , 我们可以将其视为特殊的下载器,只需要将要下载的文件 url 传递过去,下载器就会自动将文件下载到本地

简易流程

我们用伪代码说明下载器的流程,假设我们要下载以下页面中的文件


    

GEM专辑

下载《偶尔》 下载《一路逆风》 下载《来自天堂的魔鬼》

下载以上 mp3 文件的步骤如下:

  1. settings.py 中开启 FilesPipeline 以及指定下载路径
ITEM_PIPELINES = {'scrapy.pipelines.files.FilesPipeline': 1}
FILES_STORE = '/music_downloads'

FilesPipeline 要置于其他 Item Pipeline 之前

  1. Spider 解析页面,提取要下载的 url 赋给 item 的 file_urls 字段

伪代码如下:

class DownloadMusicSpider(scrapy.Spider):
    # ...
    def parse(response):
        item = {}
        # 提取 url 组装成列表,并赋给 item 的 file_urls 字段
        for url in response.xpath('//a/@href').extract():
            download_url = response.urljoin(url)
            item['file_urls'].append(download_url)
        yield item

项目实战需求分析

https://matplotlib.org/examples/index.html 是著名的 python 绘图库,每个示例都有相应的源码下载,如:https://matplotlib.org/examples/animation/basic_example_writer.html

scrapy爬虫抓取并下载文件_第1张图片
下载源码

我们的需求就是要抓取 matplotlib 的示例代码,并分门别类下载存放到本地

正式写代码之前,先用 scrapy shell 分析源码结构

$ scrapy shell http://matplotlib.org/examples/index.html
# ...
In [1]: view(response) # 将页面下载到本地,分析其 html 结构
Out[1]: True
scrapy爬虫抓取并下载文件_第2张图片
分析页面 html 结构

分析可知,所有例子链接都在

下的每一个
  • 在 scrapy shell 中提取链接

    In [2]: from scrapy.linkextractors import LinkExtractor
    
    In [3]: le = LinkExtractor(restrict_css='div.toctree-wrapper.compound li.toctree-l2')
    
    In [4]: links = le.extract_links(response)
    
    In [5]: [link.url for link in links]
    Out[5]:
    ['https://matplotlib.org/examples/animation/animate_decay.html',
     'https://matplotlib.org/examples/animation/basic_example.html',
     'https://matplotlib.org/examples/animation/basic_example_writer.html',
     'https://matplotlib.org/examples/animation/bayes_update.html',
     # ...
    ]
    

    然后再来分析具体的例子页面,提取下载源码的 url

    In [6]: fetch('https://matplotlib.org/examples/animation/animate_decay.html')
    2019-07-21 22:15:22 [scrapy.core.engine] DEBUG: Crawled (200)  (referer: None)
    
    In [7]: view(response)
    Out[7]: True
    
    scrapy爬虫抓取并下载文件_第3张图片
    下载页面 html 结构

    分析可知,下载 url 在 元素中获取

    In [8]: href = response.css('a.reference.external::attr(href)').extract_first()
    
    In [9]: href
    Out[9]: 'animate_decay.py'
    
    In [10]: response.urljoin(href) # 组装成绝对地址
    Out[10]: 'https://matplotlib.org/examples/animation/animate_decay.py'
    

    具体编码实现

    创建项目

  • 你可能感兴趣的:(scrapy爬虫抓取并下载文件)