Skip to content

Instantly share code, notes, and snippets.

@IAlwaysBeCoding
Last active September 21, 2018 04:52
Show Gist options
  • Save IAlwaysBeCoding/2dbfa1a301a4da60e677ad1f6ed0c354 to your computer and use it in GitHub Desktop.
Save IAlwaysBeCoding/2dbfa1a301a4da60e677ad1f6ed0c354 to your computer and use it in GitHub Desktop.
Scrapy Spider Middleware
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