机器人排除协议文件分析器
robotsp的Python项目详细描述
Python的Robots排除标准解析器
robots
Python模块为机器人.txt文件。建议使用的类是
robots.RobotsParser
。此外,薄立面robots.RobotFileParser
也可作为
代替^{robots.RobotFileParser
公开了一个
主要与urllib.robotparser.RobotFileParser
兼容。在
重写的主要原因如下:
- 它最初的目的是为一个链接检查器项目试验解析
robots.txt
(未实施)。在 - 它正试图遵循最新的互联网草案 Robots Exclusion Protocol。在
- 它不会试图遵从当前不存在的普遍接受的指令
specs例如
request-rate
和{}, 但它目前支持 sitemaps
。在 - 它满足与Google Robots.txt Parser相同的测试, 除了一些特定于Google机器人的定制行为。在
安装
Note:需要Python3.8.x
您最好在创建Python虚拟环境之后安装robots
包,
在新创建的目录中,如下所示:
$ mkdir project && cd project
$ python -m venv .venv --prompt robotspy
$ . .venv/bin/activate
(robotspy) $ python -m pip install --upgrade pip
(robotspy) $ python -m pip install --upgrade setuptools
(robotspy) $ python -m pip install robotspy
使用
robots
包可以作为模块导入,也可以公开可通过调用的可执行文件
python -m
。在
执行包
安装robotspy
后,可以通过运行以下命令验证安装:
示例
http://www.pythontest.net/elsewhere/robots.txt的内容如下:
# Used by NetworkTestCase in Lib/test/test_robotparser.py
User-agent: Nutch
Disallow: /
Allow: /brian/
User-agent: *
Disallow: /webstats/
要检查用户代理Nutch
是否可以获取路径/brian/
,可以执行以下操作:
(robotspy) $ python -m robots http://www.pythontest.net/elsewhere/robots.txt Nutch /brian/
user-agent 'Nutch' with URI '/brian/': ALLOWED
或者,您也可以传递完整的URL http://www.pythontest.net/brian/:
(robotspy) $ python -m robots http://www.pythontest.net/elsewhere/robots.txt Nutch /brian/
user-agent 'Nutch' with URI 'http://www.pythontest.net/brian/': ALLOWED
用户代理Nutch
能否获取路径/brian
?在
(robotspy) $ python -m robots http://www.pythontest.net/elsewhere/robots.txt Nutch /brian
user-agent 'Nutch' with URI '/brian': DISALLOWED
或者,/
?在
(robotspy) $ python -m robots http://www.pythontest.net/elsewhere/robots.txt Nutch /
user-agent 'Nutch' with URI '/': DISALLOWED
用户代理Johnny
怎么样?在
(robotspy) $ python -m robots http://www.pythontest.net/elsewhere/robots.txt Johnny /
user-agent 'Johnny' with URI '/': ALLOWED
在项目中使用模块
下面是一个使用Python shell中的robots
包的示例,其数据与上述相同:
(robotspy) $ python
>>> import robots
>>> parser = robots.RobotsParser.from_uri('http://www.pythontest.net/elsewhere/robots.txt')
>>> useragent = 'Nutch'
>>> path = '/brian/'
>>> result = parser.can_fetch(useragent, path)
>>> print(f'Can {useragent} fetch {path}? {result}')
Can Nutch fetch /brian/? True
>>>
Python标准库中的Bug
^{robotspy
使下面的测试与上面的示例不同。在
带有urllib.robotparser
的示例如下:
$ python
>>> import urllib.robotparser
>>> rp = urllib.robotparser.RobotFileParser()
>>> rp.set_url('http://www.pythontest.net/elsewhere/robots.txt')
>>> rp.read()
>>> rp.can_fetch('Nutch', '/brian/')
False
注意,结果是False
,而robotspy
返回{
Bugbpo-39187是开放的,以提高对这个问题和公关的认识
https://github.com/python/cpython/pull/17794已作为可能的修复程序提交。robotspy
没有
表现出这个问题。在
发展
主要的开发依赖项是pytest
,用于执行测试。它是自动的
如果执行以下步骤,则安装:
$ git clone https://github.com/andreburgaud/robotspy
$ cd robotspy
$ python -m venv .venv --prompt robotspy
$ . .venv/bin/activate
(robotspy) $ python -m pip install -r requirements.txt
(robotspy) $ python -m pip install -e .
(robotspy) $ make test
(robotspy) $ deactivate
$
在Windows上:
C:/> git clone https://github.com/andreburgaud/robotspy
C:/> cd robotspy
C:/> python -m venv .venv --prompt robotspy
C:/> .venv\scripts\activate
(robotspy) $ python -m pip install -r requirements.txt
(robotspy) $ python -m pip install -e .
(robotspy) $ make test
(robotspy) $ deactivate
其他依赖项用于部署到Cheese Shop(PyPI):
有关命令和参数,请参见构建文件Makefile
。在
依赖树
要显示依赖关系树:
$ pipdeptree
或者
$ make tree
要显示特定包的反向依赖树,idna
在下面的示例中:
$ pipdeptree --reverse --packages idna
归属
尽管除了Python标准库中的包之外,robotspy
没有任何依赖关系,但是有一些工具用于测试、验证、打包和部署该库。在
您可以通过咨询ATTRIBUTIONS查阅这些工具的列表、它们各自的版本、许可证和网站。在
发布历史记录
- 0.4.0:
- 修正了机器人文本被相对路径指向的问题
- 将MyPy、Black和PyLint作为依赖项进行集成,以简化跨平台开发
- 有限制造.batWindows生成文件
- Git忽略vscode文件,
tmp
目录,多个虚拟环境(.venv*
) - 修复了Windows上不区分大小写的问题
- 在Windows上测试成功
- 添加了一个分配文件并生成任务来生成它
- 升级版
pyparsing
和{}
- 0.3.3:
- 升级了
tqdm
,和cryptography
包 - 0.3.2条:
- 升级了
bleach
、tqdm
和{}包
- 升级了
- 0.3.1条:
- 更新了
idna
和wcwidth
包 - 添加了
pipdeptree
包以提供依赖关系的可见性 - 修正了
mypy
错误 - 显式忽略与常用名称相关的
pylint
错误,如f
,m
,或{}
- 更新了
- 0.3.0:更新
bleach
包以解决CVE-2020-6802 - 0.2.0:更新了文档
- 0.1.0:初始版本
许可证
- 项目
标签: