Debian Codegraph @

PKG-INFO

raw
1 Metadata-Version: 2.4
2 Name: Protego
3 Version: 0.6.2
4 Summary: Pure-Python robots.txt parser with support for modern conventions
5 Project-URL: Homepage, https://github.com/scrapy/protego
6 Project-URL: Source, https://github.com/scrapy/protego
7 Project-URL: Tracker, https://github.com/scrapy/protego/issues
8 Project-URL: Release notes, https://github.com/scrapy/protego/blob/master/CHANGELOG.rst
9 Author-email: Anubhav Patel <[email protected]>
10 License-Expression: BSD-3-Clause
11 License-File: LICENSE
12 Keywords: parser,rep,robots,robots.txt
13 Classifier: Development Status :: 4 - Beta
14 Classifier: Intended Audience :: Developers
15 Classifier: Operating System :: OS Independent
16 Classifier: Programming Language :: Python
17 Classifier: Programming Language :: Python :: 3
18 Classifier: Programming Language :: Python :: 3.10
19 Classifier: Programming Language :: Python :: 3.11
20 Classifier: Programming Language :: Python :: 3.12
21 Classifier: Programming Language :: Python :: 3.13
22 Classifier: Programming Language :: Python :: 3.14
23 Classifier: Programming Language :: Python :: Implementation :: CPython
24 Classifier: Programming Language :: Python :: Implementation :: PyPy
25 Classifier: Topic :: Internet :: WWW/HTTP
26 Classifier: Topic :: Software Development :: Libraries :: Python Modules
27 Requires-Python: >=3.10
28 Description-Content-Type: text/x-rst
29
30 =======
31 Protego
32 =======
33
34 .. image:: https://img.shields.io/pypi/pyversions/protego.svg
35 :target: https://pypi.python.org/pypi/protego
36 :alt: Supported Python Versions
37
38 .. image:: https://github.com/scrapy/protego/actions/workflows/tests-ubuntu.yml/badge.svg
39 :target: https://github.com/scrapy/protego/actions/workflows/tests-ubuntu.yml
40 :alt: CI
41
42 Protego is a pure-Python ``robots.txt`` parser with support for modern
43 conventions.
44
45
46 Install
47 =======
48
49 To install Protego, simply use pip:
50
51 .. code-block:: none
52
53 pip install protego
54
55
56 Usage
57 =====
58
59 .. code-block:: pycon
60
61 >>> from protego import Protego
62 >>> robotstxt = """
63 ... User-agent: *
64 ... Disallow: /
65 ... Allow: /about
66 ... Allow: /account
67 ... Disallow: /account/contact$
68 ... Disallow: /account/*/profile
69 ... Crawl-delay: 4
70 ... Request-rate: 10/1m # 10 requests every 1 minute
71 ...
72 ... Sitemap: http://example.com/sitemap-index.xml
73 ... Host: http://example.co.in
74 ... """
75 >>> rp = Protego.parse(robotstxt)
76 >>> rp.can_fetch("http://example.com/profiles", "mybot")
77 False
78 >>> rp.can_fetch("http://example.com/about", "mybot")
79 True
80 >>> rp.can_fetch("http://example.com/account", "mybot")
81 True
82 >>> rp.can_fetch("http://example.com/account/myuser/profile", "mybot")
83 False
84 >>> rp.can_fetch("http://example.com/account/contact", "mybot")
85 False
86 >>> rp.crawl_delay("mybot")
87 4.0
88 >>> rp.request_rate("mybot")
89 RequestRate(requests=10, seconds=60, start_time=None, end_time=None)
90 >>> list(rp.sitemaps)
91 ['http://example.com/sitemap-index.xml']
92 >>> rp.preferred_host
93 'http://example.co.in'
94
95
96 Using Protego with Requests_:
97
98 .. code-block:: pycon
99
100 >>> from protego import Protego
101 >>> import requests
102 >>> r = requests.get("https://google.com/robots.txt")
103 >>> rp = Protego.parse(r.text)
104 >>> rp.can_fetch("https://google.com/search", "mybot")
105 False
106 >>> rp.can_fetch("https://google.com/search/about", "mybot")
107 True
108 >>> list(rp.sitemaps)
109 ['https://www.google.com/sitemap.xml']
110
111 .. _Requests: https://3.python-requests.org/
112
113
114 Comparison
115 ==========
116
117 The following table compares Protego to the most popular ``robots.txt`` parsers
118 implemented in Python or featuring Python bindings:
119
120 +----------------------------+---------+-----------------+--------+---------------------------+
121 | | Protego | RobotFileParser | Reppy | Robotexclusionrulesparser |
122 +============================+=========+=================+========+===========================+
123 | Implementation language | Python | Python | C++ | Python |
124 +----------------------------+---------+-----------------+--------+---------------------------+
125 | Reference specification | Google_ | `Martijn Koster’s 1996 draft`_ |
126 +----------------------------+---------+-----------------+--------+---------------------------+
127 | `Wildcard support`_ | ✓ | | ✓ | ✓ |
128 +----------------------------+---------+-----------------+--------+---------------------------+
129 | `Length-based precedence`_ | ✓ | | ✓ | |
130 +----------------------------+---------+-----------------+--------+---------------------------+
131 | Performance_ | | +40% | +1300% | -25% |
132 +----------------------------+---------+-----------------+--------+---------------------------+
133
134 .. _Google: https://developers.google.com/search/reference/robots_txt
135 .. _Length-based precedence: https://developers.google.com/search/reference/robots_txt#order-of-precedence-for-group-member-lines
136 .. _Martijn Koster’s 1996 draft: https://www.robotstxt.org/norobots-rfc.txt
137 .. _Performance: https://anubhavp28.github.io/gsoc-weekly-checkin-12/
138 .. _Wildcard support: https://developers.google.com/search/reference/robots_txt#url-matching-based-on-path-values
139
140
141 API Reference
142 =============
143
144 Class ``protego.Protego``:
145
146 Properties
147 ----------
148
149 * ``sitemaps`` {``list_iterator``} A list of sitemaps specified in
150 ``robots.txt``.
151
152 * ``preferred_host`` {string} Preferred host specified in ``robots.txt``.
153
154
155 Methods
156 -------
157
158 * ``parse(robotstxt_body)`` Parse ``robots.txt`` and return a new instance of
159 ``protego.Protego``.
160
161 * ``can_fetch(url, user_agent)`` Return True if the user agent can fetch the
162 URL, otherwise return ``False``.
163
164 * ``crawl_delay(user_agent)`` Return the crawl delay specified for the user
165 agent as a float. If nothing is specified, return ``None``.
166
167 * ``request_rate(user_agent)`` Return the request rate specified for the user
168 agent as a named tuple ``RequestRate(requests, seconds, start_time,
169 end_time)``. If nothing is specified, return ``None``.
170
171 * ``visit_time(user_agent)`` Return the visit time specified for the user
172 agent as a named tuple ``VisitTime(start_time, end_time)``.
173 If nothing is specified, return ``None``.