作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 scrapy。在我当前的项目中,我正在从 pdf 文件中捕获文本。我想将其发送到管道进行解析。现在我有:
def get_pdf_text(self, response):
in_memory_pdf = BytesIO(bytes(response.body))
in_memory_pdf.seek(0)
doc = slate.PDF(in_memory_pdf)
item =OveItem()
item['pdf_text']=doc
return item
管道.py
class OvePipeline(object):
def process_item(self, item, spider):
.......
return item
这可行,但我认为直接生成结果会更干净,而不必将结果附加到项目上以将其获取到管道,例如:
def get_pdf_text(self, response):
in_memory_pdf = BytesIO(bytes(response.body))
in_memory_pdf.seek(0)
yield slate.PDF(in_memory_pdf)
这可能吗?
最佳答案
根据Scrapy documentation ,蜘蛛回调必须返回 Request
实例、字典或 Item
实例:
This method, as well as any other Request callback, must return an iterable of Request and/or dicts or Item objects.
因此,如果您不想为 pdf 内容定义特殊的“项目”,只需将其包装到字典中即可:
def get_pdf_text(self, response):
in_memory_pdf = BytesIO(bytes(response.body))
in_memory_pdf.seek(0)
doc = slate.PDF(in_memory_pdf)
return {'pdf_text': doc}
关于python - 将内容直接发送到 scrapy pipeline,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39810251/
我是一名优秀的程序员,十分优秀!