Python参考手册
文件格式 | File Formats
robotparser
注意
robotparser
模块已urllib.robotparser
在Python 3中重命名。当将源代码转换为Python 3时,2to3工具将自动适应导入。
该模块提供了一个类,RobotFileParser
它回答关于特定用户代理是否可以在发布该robots.txt
文件的网站上获取URL的问题。有关robots.txt
文件结构的更多详细信息,请参阅http://www.robotstxt.org/orig.html。
class robotparser.RobotFileParser(url='')
这个类提供了一些方法来读取,解析和回答有关urlrobots.txt
文件的问题。
set_url(url)
设置引用robots.txt
文件的URL 。
read()
读取robots.txt
URL并将其提供给解析器。
parse(lines)
解析行参数。
can_fetch(useragent, url)
返回True
是否允许useragent根据解析文件中包含的规则获取urlrobots.txt
。
mtime()
返回robots.txt
文件上次获取的时间。这对于需要robots.txt
定期检查新文件的长时间运行的网络蜘蛛非常有用。
modified()
设置robots.txt
文件上次获取到当前时间的时间。
以下示例演示了RobotFileParser类的基本用法。
>>> import robotparser
>>> rp = robotparser.RobotFileParser()
>>> rp.set_url("http://www.musi-cal.com/robots.txt")
>>> rp.read()
>>> rp.can_fetch("*", "http://www.musi-cal.com/cgi-bin/search?city=San+Francisco")
False
>>> rp.can_fetch("*", "http://www.musi-cal.com/")
True
文件格式 | File Formats相关
Python 是一种面向对象的解释型计算机程序设计语言,由荷兰人 Guido van Rossum 于1989年发明,第一个公开发行版发行于1991年。 Python 是纯粹的自由软件, 源代码和解释器 CPython 遵循 GPL 协议。Python 语法简洁清晰,特色之一是强制用空白符( white space )作为语句缩进。
主页 | https://www.python.org/ |
源码 | https://github.com/python/cpython |
版本 | 2.7 |
发布版本 | 2.7.13 |