我正在开发一个Scrapy爬虫,尝试从目录中的多个PDF文件中提取文本,使用slate。我没有兴趣将实际的PDF保存到磁盘上,因此有人建议我查看https://docs.python.org/2/library/io.html#buffered-streams中的io.bytesIO子类。
然而,我不确定如何将PDF正文传递给bytesIO类,然后传递虚拟PDF slate以获取文本。到目前为止,我做了以下工作:
然而,我不确定如何将PDF正文传递给bytesIO类,然后传递虚拟PDF slate以获取文本。到目前为止,我做了以下工作:
class Ove_Spider(BaseSpider):
name = "ove"
allowed_domains = ['myurl.com']
start_urls = ['myurl/hgh/']
def parse(self, response):
for a in response.xpath('//a[@href]/@href'):
link = a.extract()
if link.endswith('.pdf'):
link = urlparse.urljoin(base_url, link)
yield Request(link, callback=self.save_pdf)
def save_pdf(self, response):
in_memory_pdf = BytesIO()
in_memory_pdf.read(response.body) # Trying to read in PDF which is in response body
我得到:
in_memory_pdf.read(response.body)
TypeError: integer argument expected, got 'str'
我该如何使其工作?