3

我正在编写一个非常简单的网络爬虫并尝试解析'robots.txt'文件。我robotparser在标准库中找到了该模块,它应该可以做到这一点。我正在使用 Python 2.7.2。不幸的是,我的代码无法'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,要么无法读取文本文件。

我也在交互式解释器中尝试过。这就是发生的情况,使用与文档页面相同的语法。

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'文件的内容,但它返回空白。更令人沮丧的是,这些 示例都可以正常工作,但是当我尝试自己的 URL 时却失败了。我对 Python 很陌生,我不知道出了什么问题。据我所知,我使用该模块的方式与文档和示例相同。谢谢你的帮助!

更新 1:这里还有几行来自解释器,以防print rp不是检查是否'robots.txt'读入的好方法。pathhosturl属性是正确的,但来自的条目'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:我已经通过使用这个外部库来解析'robots.txt'文件解决了这个问题。(但我还没有回答最初的问题!)在终端上花费了更多时间后,我最好的猜测是它robotparser无法处理'robots.txt'规范中的某些添加,例如Sitemap,并且有空行问题。它将读取来自 Stack Overflow 和 Python.org 等文件,但不会读取 Google、YouTube 或我的原始 Udacity 文件,其中包括Sitemap语句和空行。如果比我聪明的人能证实或解释这一点,我仍然会很感激!

4

2 回答 2

2

我通过使用这个外部库来解析“robots.txt”文件解决了这个问题。(但我还没有回答最初的问题!)在终端上花费了更多时间后,我最好的猜测是,robotparser 无法处理对“robots.txt”规范的某些添加,例如站点地图,并且在空白行方面存在问题。它将读取来自 Stack Overflow 和 Python.org 等文件,但不会读取 Google、YouTube 或我的原始 Udacity 文件,其中包括 Sitemap 语句和空行。如果比我聪明的人能证实或解释这一点,我仍然会很感激!

于 2012-04-14T22:15:46.790 回答
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

瞧!

于 2013-03-11T18:27:59.653 回答