robots.txt kurallarını Python ile test etmek

Bir robots.txt değişikliğini canlıya almadan önce "şu URL'ler hâlâ taranabilir mi?" sorusunu tek tek tarayıcıda denemek zahmetli. Python'un standart kütüphanesinde bunun için hazır bir modül var.

robots_test.py
from urllib.robotparser import RobotFileParser

KURALLAR = """
user-agent: *
disallow: /arama
disallow: /*?sirala=
allow: /arama/yardim

user-agent: Googlebot-Image
disallow: /
"""

rp = RobotFileParser()
rp.parse(KURALLAR.splitlines())

testler = [
    ("Googlebot", "https://ornek.com/arama?q=seo"),
    ("Googlebot", "https://ornek.com/arama/yardim"),
    ("Googlebot", "https://ornek.com/urunler?sirala=fiyat"),
    ("Googlebot-Image", "https://ornek.com/resim.webp"),
]

for bot, url in testler:
    durum = "İZİN VAR" if rp.can_fetch(bot, url) else "ENGELLİ"
    print(f"{durum:9} {bot:16} {url}")

Dikkat: her ayrıştırıcı aynı değil

Python'un modülü eski taslağa göre çalışıyor ve iki noktada Google'dan farklı:

  1. Joker karakterler (*, $): Python bunları desteklemiyor. Yukarıdaki /*?sirala= kuralı Python'da işe yaramaz ama Google'da çalışır.
  2. Allow/Disallow önceliği: Google en uzun (en özel) kuralı seçer. Python ise ilk eşleşeni alır.

Yani Python ile basit kuralları hızlıca test edebilirsin. Joker karakter kullanıyorsan Google'ın açık kaynak ayrıştırıcısı ya da Search Console'daki robots.txt raporu daha güvenilir.

Canlı dosyayı test etmek

rp = RobotFileParser("https://johnflaxman.com/robots.txt")
rp.read()
print(rp.can_fetch("*", "https://johnflaxman.com/search?q=test"))

Comments / questions

There's no comment section here. If you have a question or want to add something, message me on Telegram or send an email to [email protected]. Thanks!

Related pages