How do you open a file stream for reading using Scrapy?

拥有回忆 提交于 2019-12-08 07:16:31

问题


Using Scrapy, I want to use my extracted url to read a binary file into memory and extract the contents.

Currently, I can find the URL on the page using a selector e.g.

myFile = response.xpath('//a[contains(@href,".interestingfileextension")]/@href').extract()

How do I then read that file into memory so that I can look for content in that file?

Many thanks


回答1:


Make a request and explore the content in the callback:

def parse(self, response):
    url = response.xpath('//a[contains(@href,".interestingfileextension")]/@href').extract_first()
    return scrapy.Request(url, callback=self.parse_file)

def parse_file(self, response):
    # response here is the contents of the file
    print(response.body)


来源:https://stackoverflow.com/questions/36226727/how-do-you-open-a-file-stream-for-reading-using-scrapy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!