Last active
March 3, 2023 15:24
-
-
Save joekolade/e7c27dee7cf72372dbe4a9aaaede5cfe to your computer and use it in GitHub Desktop.
[scrapy site crawler] crawls a whole site
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
| # run with | |
| # `scrapy runspider crawl-site.py` | |
| import scrapy | |
| from scrapy.spiders import CrawlSpider, Rule | |
| from scrapy.linkextractors import LinkExtractor | |
| class MySpider(CrawlSpider): | |
| name = 'domain.com' | |
| allowed_domains = ['domain.com'] | |
| start_urls = ['https://www.domain.com'] | |
| rules = ( | |
| Rule(LinkExtractor(), callback='parse_item', follow=True), | |
| ) | |
| # save to file | |
| """ | |
| def parse_item(self, response): | |
| filename = response.url.split("/")[-2] + '.html' | |
| with open(filename, 'wb') as f: | |
| f.write(response.body) | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment