跳转到主要内容

支持现代约定的纯Python robots.txt解析器

项目描述

Supported Python Versions CI

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

参考规范

Google

Martijn Koster的1996年草案

通配符支持

基于长度的优先级

性能

+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 (3.2 MB 查看哈希值)

上传时间

构建分布

Protego-0.3.1-py2.py3-none-any.whl (8.5 kB 查看哈希值)

上传时间 Python 2 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面