Skip to content

Instantly share code, notes, and snippets.

@GeroZayas
Created August 19, 2025 06:37
Show Gist options
  • Select an option

  • Save GeroZayas/cd8abef88fd8791322003943f8f00ead to your computer and use it in GitHub Desktop.

Select an option

Save GeroZayas/cd8abef88fd8791322003943f8f00ead to your computer and use it in GitHub Desktop.
from urllib.parse import urlparse
from urllib.robotparser import RobotFileParser
# Look at this cool function to respect robots.txt
USER_AGENT = (
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"
)
def _respect_robots(url: str, timeout: int = 10) -> bool:
parsed = urlparse(url)
# the scheme is something like "https"
# the netloc is the domain, e.g. www.dim.fr
robots_url = f"{parsed.scheme}://{parsed.netloc}/robots.txt"
rp = RobotFileParser()
try:
rp.set_url(robots_url)
rp.read()
except Exception:
# If robots.txt is not reachable, default to allow (common practice)
return True
return rp.can_fetch(USER_AGENT, url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment