urllib.robotparser ---robots.txt的分析器¶
源代码: Lib/urllib/robotparser.py
此模块提供一个类, RobotFileParser ,它回答有关特定用户代理是否可以在发布 robots.txt 文件。有关 robots.txt 文件,请参见http://www.robotstxt.org/orig.html。
- class urllib.robotparser.RobotFileParser(url='')¶
这个类提供了阅读、分析和回答有关
robots.txt文件在 url .- set_url(url)¶
将URL设置为引用
robots.txt文件。
- read()¶
阅读
robots.txt并将其提供给解析器。
- parse(lines)¶
分析Lines参数。
- can_fetch(useragent, url)¶
返回
True如果 字符串 允许获取 url 根据分析中包含的规则robots.txt文件。
- mtime()¶
返回
robots.txt上次提取文件。这对于需要检查新的robots.txt定期存档。
- modified()¶
设置时间
robots.txt文件上次被提取到当前时间。
- crawl_delay(useragent)¶
返回的值
Crawl-delay参数从robots.txt对于 字符串 有问题。如果没有此类参数或它不适用于 字符串 指定的或robots.txt此参数的条目语法无效,返回None.3.6 新版功能.
- request_rate(useragent)¶
返回的内容
Request-rate参数从robots.txt作为一个 named tupleRequestRate(requests, seconds). 如果没有此类参数或它不适用于 字符串 指定的或robots.txt此参数的条目语法无效,返回None.3.6 新版功能.
下面的示例演示了 RobotFileParser 类:
>>> import urllib.robotparser
>>> rp = urllib.robotparser.RobotFileParser()
>>> rp.set_url("http://www.musi-cal.com/robots.txt")
>>> rp.read()
>>> rrate = rp.request_rate("*")
>>> rrate.requests
3
>>> rrate.seconds
20
>>> rp.crawl_delay("*")
6
>>> rp.can_fetch("*", "http://www.musi-cal.com/cgi-bin/search?city=San+Francisco")
False
>>> rp.can_fetch("*", "http://www.musi-cal.com/")
True