|
15 | 15 | from typing_extensions import override |
16 | 16 |
|
17 | 17 | from crawlee import ConcurrencySettings, Request |
| 18 | +from crawlee._utils.robots import RobotsTxtFile |
18 | 19 | from crawlee.crawlers import ( |
19 | 20 | AdaptivePlaywrightCrawler, |
20 | 21 | AdaptivePlaywrightCrawlingContext, |
|
43 | 44 |
|
44 | 45 | from yarl import URL |
45 | 46 |
|
| 47 | + from crawlee._types import SkippedReason |
| 48 | + |
46 | 49 |
|
47 | 50 | _H1_TEXT = 'Static' |
48 | 51 | _H2_TEXT = 'Only in browser' |
@@ -695,6 +698,54 @@ async def request_handler(context: AdaptivePlaywrightCrawlingContext) -> None: |
695 | 698 | mocked_browser_handler.assert_called_once_with() |
696 | 699 |
|
697 | 700 |
|
| 701 | +@pytest.mark.parametrize( |
| 702 | + 'rendering_type', |
| 703 | + [ |
| 704 | + pytest.param('static', id='static'), |
| 705 | + pytest.param('client only', id='browser'), |
| 706 | + ], |
| 707 | +) |
| 708 | +async def test_on_skipped_request(server_url: URL, rendering_type: RenderingType) -> None: |
| 709 | + """Links that a sub crawler skips in `enqueue_links` because of robots.txt reach the callback of the crawler.""" |
| 710 | + crawler = AdaptivePlaywrightCrawler.with_beautifulsoup_static_parser( |
| 711 | + rendering_type_predictor=_SimpleRenderingTypePredictor( |
| 712 | + rendering_types=cycle([rendering_type]), detection_probability_recommendation=cycle([0]) |
| 713 | + ), |
| 714 | + respect_robots_txt_file=True, |
| 715 | + ) |
| 716 | + skip = Mock() |
| 717 | + |
| 718 | + @crawler.router.default_handler |
| 719 | + async def request_handler(context: AdaptivePlaywrightCrawlingContext) -> None: |
| 720 | + await context.enqueue_links() |
| 721 | + |
| 722 | + @crawler.on_skipped_request |
| 723 | + async def skipped_hook(url: str, _reason: SkippedReason) -> None: |
| 724 | + skip(url) |
| 725 | + |
| 726 | + await crawler.run([str(server_url / 'start_enqueue')]) |
| 727 | + |
| 728 | + skip.assert_has_calls([call(str(server_url / f'page_{index}')) for index in range(1, 5)], any_order=True) |
| 729 | + |
| 730 | + |
| 731 | +async def test_fetches_robots_txt_once(server_url: URL, monkeypatch: pytest.MonkeyPatch) -> None: |
| 732 | + """The crawler and its sub crawlers fetch the robots.txt file of a domain once, detection runs included.""" |
| 733 | + find = AsyncMock(wraps=RobotsTxtFile.find) |
| 734 | + monkeypatch.setattr(RobotsTxtFile, 'find', find) |
| 735 | + crawler = AdaptivePlaywrightCrawler.with_beautifulsoup_static_parser( |
| 736 | + rendering_type_predictor=_SimpleRenderingTypePredictor(detection_probability_recommendation=cycle([1])), |
| 737 | + respect_robots_txt_file=True, |
| 738 | + ) |
| 739 | + |
| 740 | + @crawler.router.default_handler |
| 741 | + async def request_handler(context: AdaptivePlaywrightCrawlingContext) -> None: |
| 742 | + await context.enqueue_links() |
| 743 | + |
| 744 | + await crawler.run([str(server_url / 'start_enqueue')]) |
| 745 | + |
| 746 | + find.assert_awaited_once() |
| 747 | + |
| 748 | + |
698 | 749 | async def test_adaptive_context_query_selector_beautiful_soup(test_urls: list[str]) -> None: |
699 | 750 | """Test that `context.query_selector_one` works regardless of the crawl type for BeautifulSoup variant. |
700 | 751 |
|
|
0 commit comments