Skip to main content

TurboCrawler

What is it?

TurboCrawler is a micro-framework that makes it easy to build your own crawlers. It is designed to be fast, highly customizable, extensible, and easy to use, giving you full control over crawler behavior. It provides tools to schedule requests, parse your data asynchronously, and extract redirect links from HTML pages.

Installation

pip install turbocrawler

Code Example

import asyncio
import json

import requests
from pydantic import BaseModel
from selectolax.lexbor import LexborHTMLParser

from turbocrawler import Crawler, CrawlerRequest, CrawlerResponse, CrawlerRunner, ExecutionInfo, ExtractRule, LoggedData
from turbocrawler.engine.control import RetryRequest


class Quote(BaseModel):
    author: str
    quote: str

class QuotesToScrapeCrawler(Crawler):
    # Lib Attributes
    crawler_name = "QuotesToScrape"
    allowed_domains = ['quotes.toscrape.com']
    regex_extract_rules = [ExtractRule(r'https://quotes.toscrape.com/page/[0-9]')]
    time_between_requests = (0.5, 1)

    # Personal Attributes
    session: requests.Session
    quote_list: list[Quote] = []


    async def start_crawler(self) -> None:
        self.session = requests.session()

    async def login(self) -> LoggedData:
        username = self.cli_kwargs["username"]
        password = self.cli_kwargs["password"]
        login_url = "https://quotes.toscrape.com/login"
        response = self.session.post(login_url, data={"username": username, "password": password}, allow_redirects=True)
        if response.status_code != 200:
            raise Exception("Login Failed")

        return LoggedData(cookies=self.session.cookies.get_dict(),
                          headers=self.session.headers,
                          local_storage={})

    async def schedule_requests(self) -> None:
        await self.crawler_queue.add(CrawlerRequest(url="https://quotes.toscrape.com/page/1/"))


    async def process_request(self, crawler_request: CrawlerRequest) -> CrawlerResponse:
        response = self.session.get(crawler_request.url)
        return CrawlerResponse(
            url=response.url,
            body=response.text,
            json={},
            status_code=response.status_code
        )

    async def process_response(self, crawler_request: CrawlerRequest, crawler_response: CrawlerResponse) -> None:
        selector = LexborHTMLParser(crawler_response.body)
        quote_list = selector.css('div[class="quote"]')
        if not quote_list:
            raise RetryRequest(reason="No quotes found", retries=2)
        crawler_response.kwargs['success'] = True

    async def parse(self, crawler_request: CrawlerRequest, crawler_response: CrawlerResponse) -> None:
        # Get values from previous process
        assert crawler_response.kwargs['success'] is True 

        selector = LexborHTMLParser(crawler_response.body)
        quote_list = selector.css('div[class="quote"]')
        for quote in quote_list:
            data = {"quote": quote.css_first('span:nth-child(1)').text()[1:-1],
                    "author": quote.css_first('span:nth-child(2)>small').text(),
                    "tag_list": [tag.text() for tag in quote.css('div[class="tags"]>a') if tag]}
            self.quote_list.append(Quote(**data))

    async def save_all(self) -> None:
        self.logger.info("All data parsed, saving to quotes.json")
        with open("quotes.json", "w") as f:
            json.dump([quote.model_dump() for quote in self.quote_list], f, indent=4)

    async def stop_crawler(self, execution_info: ExecutionInfo) -> None:
        self.session.close()

if __name__ == '__main__':
    cli_kwargs = {"username": "admin", "password": "123"}
    asyncio.run(CrawlerRunner(crawler=QuotesToScrapeCrawler, cli_kwargs=cli_kwargs).start())

How to run

You can run this command at any crawler you want to execute

asyncio.run(CrawlerRunner(crawler=QuotesToScrapeCrawler).run())

Or
You can also run it using the command line interface (CLI) by running the following command in your terminal:

turbocrawler run QuotesToScrapeCrawler --crawlers-file path/to/your/crawlers.py -username admin -password 123

Understanding TurboCrawler

Crawler

Attributes

  • crawler_name: The name of your crawler. This info will be used by CrawledQueue.
  • allowed_domains: List containing all domains that the crawler may add to CrawlerQueue.
  • regex_extract_rules: List containing ExtractRule objects. The regex passed here will be used to extract all redirect links from an HTML page (e.g., 'href="/users"') that you return in CrawlerResponse.body. If you leave this list empty, it will not enable the automatic population of CrawlerQueue for every CrawlerResponse.body.
  • time_between_requests: Time range that each request will have to wait before being executed.

Methods

start_crawler

Use this to start a session, webdriver, etc.

login (Optional)

Use this to implement the login logic for the crawler. The response will be saved at self.logged_data attribute

schedule_requests (Optional)

Use this to to schedule the first or all pages to crawl.

process_request

This method receives all scheduled requests in the CrawlerQueue.add, either added manually or by automatic scheduling with regex_extract_rules. Here you must implement all your request logic: cookies, headers, proxy, retries, etc. The method receives a CrawlerRequest and must return a CrawlerResponse. See OBS-1.

process_response

This method receives all requests made by process_request. Here you can implement any logic, such as scheduling requests, validating responses, retrying logic, etc. This method is optional.

parse

This method receives all CrawlerResponse objects from, process_request, or process_response. Here you can parse your response, extract the target fields from HTML, and dump the data (e.g., to a database).

save_all (Optional)

Use this to save all collected data at once before closing the crawler.

stop_crawler

Use this to close a session, webdriver, etc.

OBS:

  1. If regex_extract_rules is filled, the redirects specified in the rules will be scheduled in the CrawlerQueue. If not, no requests will be scheduled automatically.

Order of calls

  1. start_crawler
  2. login
  3. schedule_requests
  4. Start a loop executing the methods sequentially: process_request -> process_response -> parse (repeat until the CrawlerQueue is empty).
  5. save_all
  6. stop_crawler

CrawlerRunner

Responsible for running the Crawler, calling the methods in order, automatically scheduling your requests, and handling the queues. By default, it uses:

  • FIFOMemoryCrawlerQueue for CrawlerQueue
  • MemoryCrawledQueue for CrawledQueue

But you can change these using the built-in queues in turbocrawler.queues or by creating your own queues.


CrawlerQueue

The CrawlerQueue stores your CrawlerRequest objects, which are then removed and processed by process_request.


CrawledQueue

The CrawledQueue stores all URLs from processed CrawlerRequest objects. It prevents dispatching a request to an already crawled URL, but this behavior can be changed.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

turbocrawler-0.0.4.tar.gz (54.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

turbocrawler-0.0.4-py3-none-any.whl (24.5 kB view details)

Uploaded Python 3

File details

Details for the file turbocrawler-0.0.4.tar.gz.

File metadata

  • Download URL: turbocrawler-0.0.4.tar.gz
  • Upload date:
  • Size: 54.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"CachyOS Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for turbocrawler-0.0.4.tar.gz
Algorithm Hash digest
SHA256 1bdf2d4594dfaae8946d60d2af948cfa6ff6b6e1a22c654202579012eac9a2a7
MD5 12a6db786ba2b5bdc5316002968998bc
BLAKE2b-256 39ae477374fe2e4fe9cdad5f1fc87028eab5742994a76e1dc0c9d33e2ee3715d

See more details on using hashes here.

File details

Details for the file turbocrawler-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: turbocrawler-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 24.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"CachyOS Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for turbocrawler-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 c44afbb2675309de37f8b044df1ae4f08e66b8e7fcf11b7941b9f03d09c66f2b
MD5 e30a414a885a255c5055e761fcf4d0b5
BLAKE2b-256 5aa6dbc9ac37621827ab6fe865854123158fd703bfa82e327e74df4dbf3a626b

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 files

0.0.3

2 files

0.0.1.post1

2 files

0.0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page