Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/crawlee/_utils/robots.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from logging import getLogger
from typing import TYPE_CHECKING

from protego import Protego
Expand All @@ -15,6 +16,9 @@
from crawlee.proxy_configuration import ProxyInfo


logger = getLogger(__name__)


class RobotsTxtFile:
def __init__(
self, url: str, robots: Protego, http_client: HttpClient | None = None, proxy_info: ProxyInfo | None = None
Expand Down Expand Up @@ -56,12 +60,20 @@ async def load(cls, url: str, http_client: HttpClient, proxy_info: ProxyInfo | N
http_client: The `HttpClient` instance used to perform the network request for fetching the robots.txt file.
proxy_info: Optional `ProxyInfo` to be used when fetching the robots.txt file. If None, no proxy is used.
"""
response = await http_client.send_request(url, proxy_info=proxy_info)
body = (
b'User-agent: *\nAllow: /' if is_status_code_client_error(response.status_code) else await response.read()
)
try:
response = await http_client.send_request(url, proxy_info=proxy_info)

body = (
b'User-agent: *\nAllow: /'
if is_status_code_client_error(response.status_code)
else await response.read()
)
robots = Protego.parse(body.decode('utf-8'))

except Exception as e:
logger.warning(f'Failed to fetch from robots.txt from "{url}" with error: "{e}"')

robots = Protego.parse(body.decode('utf-8'))
robots = Protego.parse('User-agent: *\nAllow: /')

return cls(url, robots, http_client=http_client, proxy_info=proxy_info)

Expand Down
Loading