Skip to content

Instantly share code, notes, and snippets.

@joekolade
Last active March 3, 2023 15:24
Show Gist options
  • Select an option

  • Save joekolade/e7c27dee7cf72372dbe4a9aaaede5cfe to your computer and use it in GitHub Desktop.

Select an option

Save joekolade/e7c27dee7cf72372dbe4a9aaaede5cfe to your computer and use it in GitHub Desktop.
[scrapy site crawler] crawls a whole site
# 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