2011-10-05 66 views
0

我不明白如何使用robotparser模块中的解析函数。这是我试过的:使用python robotparser

In [28]: rp.set_url("http://anilattech.wordpress.com/robots.txt") 

In [29]: rp.parse("""# If you are regularly crawling WordPress.com sites please use our firehose to receive real-time push updates instead. 
# Please see http://en.wordpress.com/firehose/ for more details. 
Sitemap: http://anilattech.wordpress.com/sitemap.xml 
User-agent: IRLbot 
Crawl-delay: 3600 
User-agent: * 
Disallow: /next/ 
# har har 
User-agent: * 
Disallow: /activate/ 
User-agent: * 
Disallow: /signup/ 
User-agent: * 
Disallow: /related-tags.php 
# MT refugees 
User-agent: * 
Disallow: /cgi-bin/ 
User-agent: * 
Disallow:""") 

In [48]: rp.can_fetch("*","http://anilattech.wordpress.com/signup/") 
Out[48]: True 

看起来rp.entries是[]。我不明白什么是错的。我尝试过更简单的例子,但同样的问题。

回答

1

这里有两个问题。首先,rp.parse方法需要一个字符串列表,因此您应该将.split("\n")添加到该行。

第二个问题是*用户代理的规则存储在rp.default_entry而不是rp.entries。如果你检查你会看到它包含一个Entry对象。

我不确定谁在这里有错,但解析器的Python实现仅尊重第一个User-agent: *部分,因此在仅给出/next/的示例中是不允许的。其他不允许的行被忽略。我没有阅读规范,所以我不能说这是一个格式不正确的robots.txt文件,或者Python代码是错误的。我会假设前者。

0

嗯,我刚刚找到答案。

1。事情是这个robots.txt [来自wordpress.com]包含多个用户代理声明。 robotparser模块不支持此功能。我删除过多的User-agent: *行解决了这个问题。

2。解析的参数是Andrew所指出的列表。