feat: add `respect_robots_txt_file` option by Mantisus · Pull Request #1162 · apify/crawlee-python · GitHub
Skip to content

feat: add respect_robots_txt_file option - #1162

Merged
janbuchar merged 21 commits into
apify:masterfrom
Mantisus:respect_robots_txt
Apr 24, 2025
Merged

janbuchar merged 21 commits into
apify:masterfrom
Mantisus:respect_robots_txt

Conversation

@Mantisus

Copy link
Copy Markdown
Collaborator

Description

  • This PR implements automatic skipping of requests based on the robots.txt file. It works based on a new boolean flag in the crawler options called respect_robots_txt_file.

Issues

Testing

  • Add tests to check respect_robots_txt_file functioning in ‘EnqueueLinksFunction’ for crawlers
  • Add tests for RobotsTxtFile

@Mantisus
Mantisus requested a review from Copilot April 17, 2025 23:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a new boolean flag, respect_robots_txt_file, to automatically skip crawling disallowed URLs based on a site's robots.txt rules. Key changes include the addition of tests for robots.txt handling across multiple crawler implementations, integration of robots.txt checking in the crawling pipeline, and the implementation of a RobotsTxtFile utility.

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/unit/server_endpoints.py Added a static ROBOTS_TXT response to simulate a robots.txt file.
tests/unit/server.py Introduced a new endpoint to serve robots.txt and updated routing logic.
tests/unit/crawlers/_playwright/test_playwright_crawler.py Added tests verifying that the PlaywrightCrawler correctly respects robots.txt.
tests/unit/crawlers/_parsel/test_parsel_crawler.py Introduced tests for the ParselCrawler to validate robots.txt respect.
tests/unit/crawlers/_beautifulsoup/test_beautifulsoup_crawler.py Added tests to ensure BeautifulSoupCrawler adheres to robots.txt rules.
tests/unit/_utils/test_robots.py New tests for generating, parsing, and validating robots.txt file behavior.
src/crawlee/crawlers/_playwright/_playwright_crawler.py Integrated robots.txt enforcement in the link extraction logic.
src/crawlee/crawlers/_basic/_basic_crawler.py Updated request adding and session handling to respect robots.txt directives.
src/crawlee/crawlers/_abstract_http/_abstract_http_crawler.py Added robots.txt checking in link extraction for HTTP-based crawling.
src/crawlee/_utils/robots.py Implemented the RobotsTxtFile class for parsing and handling robots.txt data.
pyproject.toml Added dependency for protego to support robots.txt parsing.

Comment thread src/crawlee/crawlers/_playwright/_playwright_crawler.py Outdated
Comment thread src/crawlee/crawlers/_basic/_basic_crawler.py Outdated
Comment thread src/crawlee/crawlers/_abstract_http/_abstract_http_crawler.py Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Comment thread pyproject.toml
"eval-type-backport>=0.2.0",
"httpx[brotli,http2,zstd]>=0.27.0",
"more-itertools>=10.2.0",
"protego>=0.4.0",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fun to see another scrapy project here, but I guess that it guarantees some stability, so... all good.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was planning to use RobotFileParser, but it doesn't support Google's specification. 😞

Comment thread src/crawlee/_utils/robots.py Outdated
self._robots = robots
self._original_url = URL(url).origin()

@staticmethod

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer using @classmethod and the Self return type annotation

Comment thread src/crawlee/crawlers/_basic/_basic_crawler.py Outdated
Comment thread src/crawlee/crawlers/_basic/_basic_crawler.py

@vdusek vdusek left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I have a few details... And also, could you please write a new guide/example regarding this feature?

Comment thread src/crawlee/_utils/robots.py Outdated
Comment thread src/crawlee/_utils/robots.py Outdated
Comment thread src/crawlee/_utils/robots.py Outdated
@Mantisus
Mantisus requested a review from Copilot April 24, 2025 00:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request adds support for automatically skipping requests disallowed by robots.txt files. Key changes include:

  • Introducing a new boolean option (respect_robots_txt_file) across multiple crawler implementations.
  • Adding caching and locking in the BasicCrawler to optimize fetching of robots.txt files.
  • Adding new tests and examples for verifying correct respect of robots.txt rules.

Reviewed Changes

Copilot reviewed 15 out of 16 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/unit/server_endpoints.py Added a ROBOTS_TXT constant as binary content for testing robots.txt responses
tests/unit/server.py Added an endpoint and handler function for serving robots.txt
tests/unit/crawlers/_playwright/test_playwright_crawler.py Added test to verify the crawling respects robots.txt rules in the PlaywrightCrawler
tests/unit/crawlers/_parsel/test_parsel_crawler.py Added test to verify the crawling respects robots.txt rules in the ParselCrawler
tests/unit/crawlers/_beautifulsoup/test_beautifulsoup_crawler.py Added test to verify the crawling respects robots.txt rules in the BeautifulSoupCrawler
tests/unit/crawlers/_basic/test_basic_crawler.py Added a test to ensure the robots.txt fetching lock is acquired only once
tests/unit/_utils/test_robots.py Introduced tests for the RobotsTxtFile class functionality
src/crawlee/storage_clients/_memory/_request_queue_client.py Removed extraneous type ignore comment from sortedcollections import
src/crawlee/crawlers/_playwright/_playwright_crawler.py Integrated robots.txt check into the link extraction logic
src/crawlee/crawlers/_basic/_basic_crawler.py Extended BasicCrawler with respect_robots_txt_file support and caching/locking mechanisms
src/crawlee/crawlers/_abstract_http/_abstract_http_crawler.py Integrated robots.txt check in the abstract HTTP crawler’s link extraction method
src/crawlee/_utils/robots.py Added a new RobotsTxtFile class that leverages Protego for parsing and evaluating rules
pyproject.toml Updated dependencies to include protego and sortedcollections
docs/examples/code_examples/respect_robots_txt_file.py Provided an example demonstrating the usage of respect_robots_txt_file option
Files not reviewed (1)
  • docs/examples/respect_robots_txt_file.mdx: Language not supported

Comment thread src/crawlee/crawlers/_basic/_basic_crawler.py Outdated
@Mantisus
Mantisus force-pushed the respect_robots_txt branch from 48a93b1 to 41b803d Compare April 24, 2025 01:07

@vdusek vdusek left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! LGTM

Comment thread src/crawlee/crawlers/_basic/_basic_crawler.py Outdated
@janbuchar
janbuchar merged commit c23f365 into apify:master Apr 24, 2025
Pijukatel pushed a commit that referenced this pull request May 2, 2025
…cording to `robots.txt` rules (#1166)

### Description

- This PR supplements #1162 by adding an `on_skipped_request` decorator
to handle references skipped according to `robots.txt` rules

### Issues

- Closes: #1160
- Related #1162

---------

Co-authored-by: Vlada Dusek <v.dusek96@gmail.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Apify Release Bot <noreply@apify.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants