Skip to content

Instantly share code, notes, and snippets.

@LittleYenMin
Created June 29, 2019 07:47
Show Gist options
  • Save LittleYenMin/e4ac0f640269b7a3f90cb13b90ebcd6c to your computer and use it in GitHub Desktop.
Save LittleYenMin/e4ac0f640269b7a3f90cb13b90ebcd6c to your computer and use it in GitHub Desktop.
Crawler demo in scrapy
import scrapy
class QuotesSpider(scrapy.Spider):
name = 'quotes'
start_urls = [
'http://quotes.toscrape.com/tag/humor/',
]
def parse(self, response):
for quote in response.xpath('//div[contains(@class, "quote")]'):
yield {
'text': quote.xpath('span[contains(@class, text)]/text()').get(),
'author': quote.xpath('span/small/text()').get(),
}
next_page = response.xpath('//li[contains(@class, "next")]/a/@href').get()
if next_page is not None:
yield response.follow(next_page, self.parse)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment