我应该创建管道来使用scrapy保存文件吗?

编程入门 行业动态 更新时间:2024-10-23 07:33:47
本文介绍了我应该创建管道来使用scrapy保存文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要保存一个文件 (.pdf),但我不确定如何保存.我需要保存 .pdf 并将它们存储在一个目录中,就像它们存储在我正在删除它们的站点上一样.

I need to save a file (.pdf) but I'm unsure how to do it. I need to save .pdfs and store them in such a way that they are organized in a directories much like they are stored on the site I'm scraping them off.

据我所知,我需要制作一个管道,但据我所知,管道保存的项目"和项目"只是字符串/数字等基本数据.保存文件是正确使用管道,还是应该将文件保存在蜘蛛中?

From what I can gather I need to make a pipeline, but from what I understand pipelines save "Items" and "items" are just basic data like strings/numbers. Is saving files a proper use of pipelines, or should I save file in spider instead?

推荐答案

是和否[1].如果您获取 pdf 文件,它将存储在内存中,但如果 pdf 文件不够大,无法填满您的可用内存,则可以.

Yes and no[1]. If you fetch a pdf it will be stored in memory, but if the pdfs are not big enough to fill up your available memory so it is ok.

您可以将 pdf 保存在蜘蛛回调中:

You could save the pdf in the spider callback:

def parse_listing(self, response): # ... extract pdf urls for url in pdf_urls: yield Request(url, callback=self.save_pdf) def save_pdf(self, response): path = self.get_path(response.url) with open(path, "wb") as f: f.write(response.body)

如果您选择在管道中进行:

If you choose to do it in a pipeline:

# in the spider def parse_pdf(self, response): i = MyItem() i['body'] = response.body i['url'] = response.url # you can add more metadata to the item return i # in your pipeline def process_item(self, item, spider): path = self.get_path(item['url']) with open(path, "wb") as f: f.write(item['body']) # remove body and add path as reference del item['body'] item['path'] = path # let item be processed by other pipelines. ie. db store return item

[1] 另一种方法可能是只存储 pdf 的 url,并使用另一个进程来获取文档而不缓冲到内存中.(例如 wget)

[1] another approach could be only store pdfs' urls and use another process to fetch the documents without buffering into memory. (e.g. wget)

更多推荐

我应该创建管道来使用scrapy保存文件吗?

本文发布于:2023-07-07 22:16:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1068532.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:道来   保存文件   scrapy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!