How to check robots.txt rules in Python

Python's standard library has urllib.robotparser: point RobotFileParser at a robots.txt URL, call read(), then can_fetch(user_agent, url) returns True or False. It applies rules in file order and doesn't understand the * and $ wildcards Google supports, so for modern files use Protego (the parser Scrapy uses) or an API. Our robots.txt Checker API answers with Google's rules, names the rule and line that decided it, and checks 16 search and AI crawlers at once for $0.003.

Try it free robots.txt Checker API — allowed or blocked per bot, $0.003 · $0.003 / call

The standard library way

It's enough for simple files with plain path prefixes. read() uses urllib with its default User-Agent, so a site that blocks Python-urllib may hand you an error page instead of the file.

from urllib.robotparser import RobotFileParser

rp = RobotFileParser("https://en.wikipedia.org/robots.txt")
rp.read()
print(rp.can_fetch("GPTBot", "https://en.wikipedia.org/wiki/Python"))
print(rp.crawl_delay("*"))   # None when not set
print(rp.site_maps())         # Python 3.8+

Where robotparser and Google disagree

If you crawl real sites, these cases come up. Protego (pip install protego) follows the modern conventions and is what Scrapy uses by default.

Rule in robots.txtGoogleurllib.robotparser
Disallow: /*.pdf$Blocks PDFs anywhereTreats * and $ as literal characters
Allow: /a/b then Disallow: /a/a/b allowed (longest match wins)Allowed, because Allow comes first
Disallow: /a then Allow: /a/b/a/b allowed (longest match wins)Blocked, because the first matching rule wins

The API: verdict, reason and 16 bots

Send a URL and a user_agent. We fetch /robots.txt from that origin, pick the most specific user-agent group, apply the longest matching rule with Allow winning a tie, and support * and $. You get allowed, matched_group, matched_rule (directive, pattern and line number), crawl_delay, the declared sitemaps and the same verdict for Googlebot, Bingbot, GPTBot, ChatGPT-User, OAI-SearchBot, ClaudeBot, Claude-User, Claude-SearchBot, Google-Extended, PerplexityBot, CCBot, Applebot, Applebot-Extended, Bytespider, meta-externalagent and Amazonbot.

Warnings catch files served as HTML, files over 500 KB (Google reads only the first 500 KB), redirects and server errors; a 5xx means Google treats the site as fully disallowed until the file comes back.

r = pay_session.post("https://iamai.markets/api/x402/services/tCZIkybWQ0S0", json={
    "url": "https://en.wikipedia.org/w/index.php?title=X&action=edit",
    "user_agent": "GPTBot",
})
res = r.json()["result"]
print(res["allowed"], res["matched_rule"])
blocked = [b["agent"] for b in res["bots"] if not b["allowed"]]
print("blocked for:", blocked)

# $0.003 per call via x402 (USDC on Base); 5 free tries a day on the listing page

Remember what robots.txt is

It's a request, not a lock. Well-behaved crawlers follow it, others ignore it, and a page blocked in robots.txt can still appear in search results if other sites link to it. To keep a page out of results, allow crawling and use a noindex tag instead.

The product

Questions

Does a missing robots.txt block crawlers?

No. A 404 means everything may be crawled; our API returns allowed true with a note saying so.

How do I check if a site blocks AI crawlers?

Ask for its root URL and read the bots list, which covers GPTBot, ClaudeBot, PerplexityBot, CCBot, Google-Extended and others in one call.

Can I test a robots.txt before I upload it?

Not with our API, which reads the live file. robotparser's parse() method takes lines of text, so you can test a draft locally.

Related

Updated 2026-12-02