from scrapy.pipelines.files import FilesPipeline # 自定义一个类,继承FilesPipeline这个父类 class MusicDownloadPipeline(FilesPipeline): # item['url']为音乐请求地址,item['name']为音乐名 def get_media_requests(self, item, info): yield scrapy.Request(item['url'], meta={'name': item['name']}) def file_path(self, request, response=None, info=None, *, item=None): filename=request.meta['name']+'.mp3' return filename def item_completed(self, results, item, info): return item
FILES_STORE = 'E:/我的文件/好听音乐' # 保存文件的路径
后面打开管道,执行爬虫程序就可以进行下载了