scrapy爬虫抓取并下载文件

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

简易流程

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

<body>
    <h3>GEM专辑</h3>
    <a href="/music/1.mp3">下载《偶尔》</a>
    <a href="/music/2.mp3">下载《一路逆风》</a>
    <a href="/music/3.mp3">下载《来自天堂的魔鬼》</a>
</body>

下载以上 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

下载源码

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

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

$ scrapy shell http://matplotlib.org/examples/index.html
# ...
In [1]: view(response) # 将页面下载到本地,分析其 html 结构
Out[1]: True
分析页面 html 结构

分析可知,所有例子链接都在 <div class="toctree-wrapper compound"> 下的每一个 <li class="toctree-l2">

在 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) <GET https://matplotlib.org/examples/animation/animate_decay.html> (referer: None)

In [7]: view(response)
Out[7]: True
下载页面 html 结构

分析可知,下载 url 在 <a class="reference external"> 元素中获取

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'

具体编码实现

创建项目

还有 43% 的精彩内容
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
支付 ¥2.99 继续阅读

相关阅读更多精彩内容

  • 本主题主要是scrapy入门,包含内容如下:  1. Scrapy框架环境搭建;  2. 理解scrapy框架结...
    杨强AT南京阅读 1,542评论 0 10
  • 序言第1章 Scrapy介绍第2章 理解HTML和XPath第3章 爬虫基础 第4章 从Scrapy到移动应用第5...
    SeanCheney阅读 4,473评论 5 9
  • 说起写爬虫,大多数第一时间想到的就是python了。python语法简洁明了,加上及其丰富好用的库,用它来写爬虫有...
    疯狂的哈丘阅读 8,394评论 1 15
  • 老赵并不老,平添了几分亲切。皮肤粉嫩像雪,头发是从来没有烫过的处女毛,发根处微微卷翘,蓬松地如蒲公英,一看,就有少...
    伊凡lvan阅读 382评论 0 4
  • 春天 是一场泥土的狂欢 是百花的盛会 也是苏醒与沉睡的闹钟 只有那夜里 万籁俱寂的夜里 风知道 云知道 那柳树的嫩...
    柏浅歌阅读 175评论 3 26

友情链接更多精彩内容