Last active
September 21, 2018 04:52
-
-
Save IAlwaysBeCoding/2dbfa1a301a4da60e677ad1f6ed0c354 to your computer and use it in GitHub Desktop.
Scrapy Spider Middleware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from scrapy import Request | |
class SynchronousSpiderMiddleware(object): | |
def process_spider_output(self, response, result, spider): | |
listing_request = None | |
for request in result: | |
step = request.meta.get('step', None) | |
if step == 'parse': | |
if not listing_request: | |
listing_request = request | |
listings = listing_request.meta.get('listings', []) | |
listings.append(request) | |
continue | |
elif step == 'result': | |
listings = request.meta.get('listings', []) | |
request = listings.pop(0) | |
yield request | |
if step == 'parse': | |
yield listing_request |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment