我正在写一个非常简单的网络爬虫,并试图解析'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'
在读好方法的path
,host
和url
属性是正确的,但是从条目。通过使用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
陈述和空行。如果有人比我更聪明,我仍然可以欣赏它,可以证实或解释这一点!
顺便说一句,你可以在上下文[这里](https://github.com/ecmendenhall/DaveDaveFind/blob/master/crawler/udacity_crawler.py)中看到这段代码,以防我遗漏了相关的东西。 – ecmendenhall 2012-04-05 10:18:47
行打印rp应该打印'robots.txt'文件的内容 - 你确定吗? – Hoff 2012-04-05 10:40:53
很确定。当我使用我连接的外部示例时,这是它的表现。为了以防万一,我使用翻译的更多信息更新了我的问题。 URL属性都看起来正确,但是“entries”是一个空列表。 – ecmendenhall 2012-04-05 10:55:52