Created
October 7, 2018 01:12
-
-
Save Ivanca/f10c26df03a9595f2f10c95cf300bc66 to your computer and use it in GitHub Desktop.
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
import scrapy | |
class BrickSetSpider(scrapy.Spider): | |
name = 'brick_spider' | |
start_urls = ['http://brickset.com/sets/year-2018'] | |
def parse(self, response): | |
SET_SELECTOR = '.set' | |
for brickset in response.css(SET_SELECTOR): | |
NAME_SELECTOR = 'h1 a ::text' | |
PIECES_SELECTOR = './/dl[dt/text() = "Pieces"]/dd/a/text()' | |
MINIFIGS_SELECTOR = './/dl[dt/text() = "Minifigs"]/dd[2]/a/text()' | |
IMAGE_SELECTOR = 'img ::attr(src)' | |
yield { | |
'name': brickset.css(NAME_SELECTOR).extract_first(), | |
'pieces': brickset.xpath(PIECES_SELECTOR).extract_first(), | |
'minifigs': brickset.xpath(MINIFIGS_SELECTOR).extract_first(), | |
'image': brickset.css(IMAGE_SELECTOR).extract_first(), | |
} | |
NEXT_PAGE_SELECTOR = '.next a ::attr(href)' | |
next_page = response.css(NEXT_PAGE_SELECTOR).extract_first() | |
if next_page: | |
yield scrapy.Request( | |
response.urljoin(next_page), | |
callback=self.parse | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment