Skip to main content
 首页 » 编程设计

Python 的 robotparser 忽略站点地图

2024年08月12日35kevingrace

我有以下 robots.txt

User-agent: * 
Disallow: /images/ 
Sitemap: http://www.example.com/sitemap.xml 

和下面的robotparser

def init_robot_parser(URL): 
    robot_parser = robotparser.RobotFileParser() 
    robot_parser.set_url(urlparse.urljoin(URL, "robots.txt")) 
    robot_parser.read() 
 
    return robot_parser 

但是当我在 return robot_parser 上面执行 print robot_parser 时,我得到的只是

User-agent: * 
Disallow: /images/ 

为什么它会忽略 Sitemap 行,我是不是遗漏了什么?

请您参考如下方法:

Sitemap 是标准的扩展,robotparser 不支持它。你可以在the source中看到它只处理“用户代理”、“禁止”和“允许”。对于其当前功能(告诉您是否允许特定 URL),无需了解 Sitemap。