Created
February 22, 2022 07:33
-
-
Save Pradip-p/c0fe28d933fa0c0860116019b42120c6 to your computer and use it in GitHub Desktop.
One page Scrapy Python
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 | |
from scrapy.crawler import CrawlerProcess | |
from scrapy.utils.project import get_project_settings | |
class LazyCrawler(scrapy.Spider): | |
name = "quotes" | |
def start_requests(self): | |
url = 'http://quotes.toscrape.com/' | |
tag = getattr(self, 'tag', None) | |
if tag is not None: | |
url = url + 'tag/' + tag | |
yield scrapy.Request(url, self.parse) | |
def parse(self, response): | |
# to_browser(response) | |
for quote in response.css('div.quote'): | |
yield { | |
'author': quote.css('small.author::text').get(), | |
'text': quote.css('span.text::text').get(), | |
'tag':quote.css('.tags a.tag::text').get(), | |
} | |
next_page = response.css('li.next a::attr(href)').get() | |
if next_page is not None: | |
yield response.follow(next_page, self.parse) | |
process = CrawlerProcess(get_project_settings()) | |
process.crawl(LazyCrawler) | |
process.start() # the script will block here until the crawling is finished |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment