Scrapy - print pipeline data in a script's context
<p>After using <code>scrapy</code> <code>framework</code>, I would like to process my <code>pipeline.py</code> output within my <code>python</code> script context.</p>

<p>The pipeline output is <code>tracks.jl</code>, as follows:</p>

<p><strong>pipeline.py</strong></p>

<pre><code>class PitchforkTracks(object):

    def __init__(self):
        self.file = open('tracks.jl', 'wb')
</code></pre>

<p>a <code>json</code> file is generated on the same <code>script.py</code> <code>directory</code>:</p>

<pre><code>    def process_item(self, item, spider):
        line = json.dumps(dict(item)) + "\n"
        self.file.write(line)
        return item
</code></pre>

<p>here, I run <code>scrapy</code>.</p>

<p><strong>script.py</strong></p>

<pre><code>def output():
    process = CrawlerProcess(get_project_settings())
    response = process.crawl('pitchfork_tracks', domain='pitchfork.com')
    # the script will block here until the crawling is finished
    process.start() 
    #process pipelined file 
    tracks = []
    time.sleep(5)
    #here I pause so there is time for pipeline to build `tracks.jl` at directory
    with open('tracks.jl', 'r+') as t:
        for line in t:
            tracks.append(json.loads(line))
    print (tracks)

    #process lines from here on
</code></pre>

<p>when I print <code>lines</code>, however, I get an empty <code>list</code> <code>[]</code>. </p>

<p>how do I print inside <code>python script</code> the output of <code>pipeline.py</code>?</p>

<p>EDIT:</p>

<p>this is my <code>log</code>when I run <code>scrapy crawl pitchfork_tracks</code>:</p>

<pre><code>2016-10-03 09:28:39 [scrapy] INFO: Scrapy 1.1.2 started (bot: blogs)
2016-10-03 09:28:39 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'blogs.spiders', 'SPIDER_MODULES': ['blogs.spiders'], 'ROBOTSTXT_OBEY': True, 'BOT_NAME': 'blogs'}
2016-10-03 09:28:39 [scrapy] INFO: Enabled extensions:
['scrapy.extensions.logstats.LogStats',
 'scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.corestats.CoreStats']
2016-10-03 09:28:39 [scrapy] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
 'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
 'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
 'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
 'scrapy.downloadermiddlewares.retry.RetryMiddleware',
 'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
 'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
 'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
 'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
 'scrapy.downloadermiddlewares.chunked.ChunkedTransferMiddleware',
 'scrapy.downloadermiddlewares.stats.DownloaderStats']
2016-10-03 09:28:39 [scrapy] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
 'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
 'scrapy.spidermiddlewares.referer.RefererMiddleware',
 'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
 'scrapy.spidermiddlewares.depth.DepthMiddleware']
2016-10-03 09:28:39 [scrapy] INFO: Enabled item pipelines:
['blogs.pipelines.PitchforkTracks',
 'blogs.pipelines.PitchforkAlbums',
 'blogs.pipelines.PitchforkReissues']
2016-10-03 09:28:39 [scrapy] INFO: Spider opened
2016-10-03 09:28:39 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2016-10-03 09:28:39 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2016-10-03 09:28:39 [scrapy] DEBUG: Crawled (200) &lt;GET http://pitchfork.com/robots.txt&gt; (referer: None)
2016-10-03 09:28:40 [scrapy] DEBUG: Crawled (200) &lt;GET http://pitchfork.com/reviews/best/tracks/?page=1&gt; (referer: None)
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from &lt;200 http://pitchfork.com/reviews/best/tracks/?page=1&gt;
{'artist': u'Moses Sumney', 'track': u'Lonely World'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from &lt;200 http://pitchfork.com/reviews/best/tracks/?page=1&gt;
{'artist': u'Cherry Glazerr', 'track': u"Told You I'd Be With the Guys"}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from &lt;200 http://pitchfork.com/reviews/best/tracks/?page=1&gt;
{'artist': u'Danny Brown',
 'track': u'Really Doe\u201d [ft. Kendrick Lamar, Ab-Soul, and Earl Sweatshirt]'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from &lt;200 http://pitchfork.com/reviews/best/tracks/?page=1&gt;
{'artist': u'NxWorries', 'track': u'Lyk Dis'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from &lt;200 http://pitchfork.com/reviews/best/tracks/?page=1&gt;
{'artist': u'Nick Cave &amp; the Bad Seeds', 'track': u'I Need You'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from &lt;200 http://pitchfork.com/reviews/best/tracks/?page=1&gt;
{'artist': u'Merchandise', 'track': u'Lonesome Sound'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from &lt;200 http://pitchfork.com/reviews/best/tracks/?page=1&gt;
{'artist': u'Weyes Blood', 'track': u'Do You Need My Love'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from &lt;200 http://pitchfork.com/reviews/best/tracks/?page=1&gt;
{'artist': u'Kelly Lee Owens', 'track': u'CBM'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from &lt;200 http://pitchfork.com/reviews/best/tracks/?page=1&gt;
{'artist': u'Sampha', 'track': u'Blood on Me'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from &lt;200 http://pitchfork.com/reviews/best/tracks/?page=1&gt;
{'artist': u'Bon Iver', 'track': u'33 \u2018GOD\u2019'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from &lt;200 http://pitchfork.com/reviews/best/tracks/?page=1&gt;
{'artist': u'Mick Jenkins', 'track': u'Drowning\u201d [ft. BADBADNOTGOOD]'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from &lt;200 http://pitchfork.com/reviews/best/tracks/?page=1&gt;
{'artist': u'Chromatics', 'track': u'Dear Tommy'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from &lt;200 http://pitchfork.com/reviews/best/tracks/?page=1&gt;
{'artist': u'Young Thug', 'track': u'Kanye West'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from &lt;200 http://pitchfork.com/reviews/best/tracks/?page=1&gt;
{'artist': u'Vince Staples', 'track': u'Prima Donna\u201d [ft. A$AP Rocky]'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from &lt;200 http://pitchfork.com/reviews/best/tracks/?page=1&gt;
{'artist': u'Angel Olsen', 'track': u'Sister'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from &lt;200 http://pitchfork.com/reviews/best/tracks/?page=1&gt;
{'artist': u'Cass McCombs', 'track': u'Bum Bum Bum'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from &lt;200 http://pitchfork.com/reviews/best/tracks/?page=1&gt;
{'artist': u'Hamilton Leithauser', 'track': u'A 1000 Times'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from &lt;200 http://pitchfork.com/reviews/best/tracks/?page=1&gt;
{'artist': u'Danny Brown', 'track': u'Pneumonia'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from &lt;200 http://pitchfork.com/reviews/best/tracks/?page=1&gt;
{'artist': u'Frank Ocean', 'track': u'Ivy'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from &lt;200 http://pitchfork.com/reviews/best/tracks/?page=1&gt;
{'artist': u'Frank Ocean', 'track': u'Rushes'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from &lt;200 http://pitchfork.com/reviews/best/tracks/?page=1&gt;
{'artist': u'Half Waif', 'track': u'Turn Me Around'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from &lt;200 http://pitchfork.com/reviews/best/tracks/?page=1&gt;
{'artist': u'Rae Sremmurd', 'track': u'Black Beatles\u201d [ft. Gucci Mane]'}
2016-10-03 09:28:40 [scrapy] DEBUG: Scraped from &lt;200 http://pitchfork.com/reviews/best/tracks/?page=1&gt;
{'artist': u'Bon Iver',
 'track': u'22 (OVER S\u221e\u221eN) [Bob Moose Extended Cab Version]'}
2016-10-03 09:28:40 [scrapy] INFO: Closing spider (finished)
2016-10-03 09:28:40 [scrapy] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 459,
 'downloader/request_count': 2,
 'downloader/request_method_count/GET': 2,
 'downloader/response_bytes': 22624,
 'downloader/response_count': 2,
 'downloader/response_status_count/200': 2,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2016, 10, 3, 12, 28, 40, 425137),
 'item_scraped_count': 23,
 'log_count/DEBUG': 26,
 'log_count/INFO': 7,
 'response_received_count': 2,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'start_time': datetime.datetime(2016, 10, 3, 12, 28, 39, 535638)}
2016-10-03 09:28:40 [scrapy] INFO: Spider closed (finished)
</code></pre>

<p>I think it's because your pipeline never closes the file object. You should open a file in your pipeline's <code>open_spider()</code> method and close it in <code>close_spider()</code> method.</p>

<pre><code>class PitchforkTracks(object):

    def open_spider(self, spider):
        self.file = open('tracks.jl', 'wb')

    def close_spider(self, spider):
        self.file.close()

    def process_item(self, item, spider):
        line = json.dumps(dict(item)) + "\n"
        self.file.write(line)
        return item
</code></pre>

python scrapy