支持现代约定的纯Python robots.txt解析器
项目描述
Protego是一个纯Python的robots.txt解析器,支持现代约定。
安装
要安装Protego,只需使用pip
pip install protego
使用方法
>>> from protego import Protego >>> robotstxt = """ ... User-agent: * ... Disallow: / ... Allow: /about ... Allow: /account ... Disallow: /account/contact$ ... Disallow: /account/*/profile ... Crawl-delay: 4 ... Request-rate: 10/1m # 10 requests every 1 minute ... ... Sitemap: http://example.com/sitemap-index.xml ... Host: http://example.co.in ... """ >>> rp = Protego.parse(robotstxt) >>> rp.can_fetch("http://example.com/profiles", "mybot") False >>> rp.can_fetch("http://example.com/about", "mybot") True >>> rp.can_fetch("http://example.com/account", "mybot") True >>> rp.can_fetch("http://example.com/account/myuser/profile", "mybot") False >>> rp.can_fetch("http://example.com/account/contact", "mybot") False >>> rp.crawl_delay("mybot") 4.0 >>> rp.request_rate("mybot") RequestRate(requests=10, seconds=60, start_time=None, end_time=None) >>> list(rp.sitemaps) ['http://example.com/sitemap-index.xml'] >>> rp.preferred_host 'http://example.co.in'
使用Protego与Requests
>>> from protego import Protego >>> import requests >>> r = requests.get("https://google.com/robots.txt") >>> rp = Protego.parse(r.text) >>> rp.can_fetch("https://google.com/search", "mybot") False >>> rp.can_fetch("https://google.com/search/about", "mybot") True >>> list(rp.sitemaps) ['https://www.google.com/sitemap.xml']
比较
下表比较了Protego与以下在Python中实现或具有Python绑定的最流行的robots.txt解析器:
Protego |
RobotFileParser |
Reppy |
Robotexclusionrulesparser |
|
---|---|---|---|---|
实现语言 |
Python |
Python |
C++ |
Python |
参考规范 |
||||
✓ |
✓ |
✓ |
||
✓ |
✓ |
|||
+40% |
+1300% |
-25% |
API参考
类 protego.Protego
属性
sitemaps {list_iterator} 在robots.txt中指定的sitemaps列表。
preferred_host {string} 在robots.txt中指定的首选主机。
方法
parse(robotstxt_body) 解析robots.txt并返回一个新的protego.Protego实例。
can_fetch(url, user_agent) 如果用户代理可以抓取URL,则返回True,否则返回False。
crawl_delay(user_agent) 返回为用户代理指定的爬取延迟,以浮点数形式表示。如果没有指定,则返回None。
request_rate(user_agent) 返回为用户代理指定的请求速率,以命名元组RequestRate(requests, seconds, start_time, end_time)形式表示。如果没有指定,则返回None。
visit_time(user_agent) 返回为用户代理指定的访问时间,以命名元组VisitTime(start_time, end_time)形式表示。如果没有指定,则返回None。
项目详情
下载文件
下载适用于您的平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。
源分布
构建分布
Protego-0.3.1.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | e94430d0d25cbbf239bc849d86c5e544fbde531fcccfa059953c7da344a1712c |
|
MD5 | 200c5f8947240a59ecee2b12efd26fd5 |
|
BLAKE2b-256 | 8a12cab9fa77ff4e9e444a5eb5480db4b4f872c03aa079145804aa054be377bc |
Protego-0.3.1-py2.py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 2fbe8e9b7a7dbc5016a932b14c98d236aad4c29290bbe457b8d2779666ef7a41 |
|
MD5 | 68ec8dbe4fd0f1481eb2b8d1ca9ff839 |
|
BLAKE2b-256 | 74efece78585a5a189d8cc2b4c2d2b92a0dc025f156a6501159b026472ebbedc |