2012-04-05 77 views
3

我正在写一个非常简单的网络爬虫,并试图解析'robots.txt'文件。我在标准库中找到了robotparser模块,应该这样做。我正在使用Python 2.7.2。不幸的是,我的代码不会正确加载'robots.txt'文件,我不知道为什么。Python robotparser模块不会加载'robots.txt'

这里是我的代码的相关片段:

from urlparse import urlparse, urljoin 
import robotparser 

def get_all_links(page, url): 
    links = [] 
    page_url = urlparse(url) 
    base = page_url[0] + '://' + page_url[1] 
    robots_url = urljoin(base, '/robots.txt') 
    rp = robotparser.RobotFileParser() 
    rp.set_url(robots_url) 
    rp.read() 
    for link in page.find_all('a'): 
     link_url = link.get('href') 
     print "Found a link: ", link_url 
     if not rp.can_fetch('*', link_url): 
      print "Page off limits!" 
      pass 

这里page是一个解析BeautifulSoup对象和url存储为一个字符串的URL。解析器读取空白的'robots.txt'文件,而不是指定URL处的文件,并将True返回给所有can_fetch()查询。它看起来好像没有打开URL或者没有阅读文本文件。

我也在交互式解释器中试过了。这是发生了什么,使用与documentation页面相同的语法。

Python 2.7.2 (default, Aug 18 2011, 18:04:39) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import robotparser 
>>> url = 'http://www.udacity-forums.com/robots.txt' 
>>> rp = robotparser.RobotFileParser() 
>>> rp.set_url(url) 
>>> rp.read() 
>>> print rp 

>>> 

线print rp应该打印'robots.txt'文件的内容,但它返回空白。更令人沮丧的是,theseexamples这两个工作完全正常,但我尝试自己的URL时失败。我对Python非常陌生,我无法弄清楚发生了什么问题。据我所知,我使用模块的方式与文档和示例相同。谢谢你的帮助!

更新1:这里是解释几行,万一print rp没有检查是否'robots.txt'在读好方法的pathhosturl属性是正确的,但是从条目。通过使用this external library解析'robots.txt'文件我已经解决了这个问题:'robots.txt'仍未读取

>>> rp 
<robotparser.RobotFileParser instance at 0x1004debd8> 
>>> dir(rp) 
['__doc__', '__init__', '__module__', '__str__', '_add_entry', 'allow_all', 'can_fetch', 'default_entry', 'disallow_all', 'entries', 'errcode', 'host', 'last_checked', 'modified', 'mtime', 'parse', 'path', 'read', 'set_url', 'url'] 
>>> rp.path 
'/robots.txt' 
>>> rp.host 
'www.udacity-forums.com' 
>>> rp.entries 
[] 
>>> rp.url 
'http://www.udacity-forums.com/robots.txt' 
>>> 

更新2。 (但我还没有回答原来的问题!)花了一些时间在终端后,我最好的猜测是robotparser不能处理'robots.txt'规范的某些增加,如Sitemap,并且空白行有问题。它将从例如文件中读取。 Stack Overflow和Python.org,但不包括Google,YouTube或我的原始Udacity文件,其中包括Sitemap陈述和空行。如果有人比我更聪明,我仍然可以欣赏它,可以证实或解释这一点!

+0

顺便说一句,你可以在上下文[这里](https://github.com/ecmendenhall/DaveDaveFind/blob/master/crawler/udacity_crawler.py)中看到这段代码,以防我遗漏了相关的东西。 – ecmendenhall 2012-04-05 10:18:47

+0

行打印rp应该打印'robots.txt'文件的内容 - 你确定吗? – Hoff 2012-04-05 10:40:53

+0

很确定。当我使用我连接的外部示例时,这是它的表现。为了以防万一,我使用翻译的更多信息更新了我的问题。 URL属性都看起来正确,但是“entries”是一个空列表。 – ecmendenhall 2012-04-05 10:55:52

回答

2

我已经通过使用这个外部库解析'robots.txt'文件解决了这个问题。 (但我还没有回答原始问题!)在终端上花费更多时间后,我最好的猜测是robotparser无法处理'robots.txt'规范的某些附加内容,例如Sitemap,并且在空白行中出现问题。它将从例如文件中读取。 Stack Overflow和Python.org,但不包括Google,YouTube或我的原始Udacity文件,其中包括站点地图声明和空白行。如果有人比我更聪明,我仍然可以欣赏它,可以证实或解释这一点!

0

的溶液可以使用reppy模块

pip install reppy 

以下是几个例子;

In [1]: import reppy 

In [2]: x = reppy.fetch("http://google.com/robots.txt") 

In [3]: x.atts 
Out[3]: 
{'agents': {'*': <reppy.agent at 0x1fd9610>}, 
'sitemaps': ['http://www.gstatic.com/culturalinstitute/sitemaps/www_google_com_culturalinstitute/sitemap-index.xml', 
    'http://www.google.com/hostednews/sitemap_index.xml', 
    'http://www.google.com/sitemaps_webmasters.xml', 
    'http://www.google.com/ventures/sitemap_ventures.xml', 
    'http://www.gstatic.com/dictionary/static/sitemaps/sitemap_index.xml', 
    'http://www.gstatic.com/earth/gallery/sitemaps/sitemap.xml', 
    'http://www.gstatic.com/s2/sitemaps/profiles-sitemap.xml', 
    'http://www.gstatic.com/trends/websites/sitemaps/sitemapindex.xml']} 

In [4]: x.allowed("/catalogs/about", "My_crawler") # Should return True, since it's allowed. 
Out[4]: True 

In [5]: x.allowed("/catalogs", "My_crawler") # Should return False, since it's not allowed. 
Out[5]: False 

In [7]: x.allowed("/catalogs/p?", "My_crawler") # Should return True, since it's allowed. 
Out[7]: True 

In [8]: x.refresh() # Refresh robots.txt, perhaps a magic change? 

In [9]: x.ttl 
Out[9]: 3721.3556718826294 

瞧!