Skip to content

Commit 40444c2

Browse files
committed
gh-60055: rebase onto main and document Request support
1 parent 6464399 commit 40444c2

2 files changed

Lines changed: 31 additions & 13 deletions

File tree

Doc/library/urllib.robotparser.rst

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ website that published the :file:`robots.txt` file. For more details on the
2121
structure of :file:`robots.txt` files, see :rfc:`9309`.
2222

2323

24-
.. class:: RobotFileParser(url='')
24+
.. class:: RobotFileParser(url_or_request='')
2525

2626
This class provides methods to read, parse and answer questions about the
27-
:file:`robots.txt` file at *url*.
27+
:file:`robots.txt` file at *url* or a :class:`Request` object with additional
28+
user-agent headers populated.
2829

29-
.. method:: set_url(url)
3030

31-
Sets the URL referring to a :file:`robots.txt` file.
31+
.. method:: set_url(url_or_request)
32+
33+
Sets the URL referring to a :file:`robots.txt` file or a :class:`Request`
34+
object with additional user-agent headers populated.
3235

3336
.. method:: read()
3437

@@ -102,3 +105,16 @@ class::
102105
True
103106
>>> rp.can_fetch("*", "http://www.pythontest.net/no-robots-here/")
104107
False
108+
109+
110+
The following example demonstrates use of a :class:`Request` object with additional user-agent headers populated::
111+
112+
>>> import urllib.robotparser
113+
>>> import urllib.request
114+
>>> rp = urllib.robotparser.RobotFileParser()
115+
>>> rp.set_url(urllib.request.Request("http://en.wikipedia.org/robots.txt", headers={"User-Agent": "IsraBot"}))
116+
>>> rp.read()
117+
>>> rp.can_fetch("*", "https://en.wikipedia.org/")
118+
True
119+
>>> rp.can_fetch("*", "https://en.wikipedia.org/trap/")
120+
False

Lib/urllib/robotparser.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ class RobotFileParser:
2727
2828
"""
2929

30-
def __init__(self, url=''):
30+
def __init__(self, url_or_request=''):
3131
self.entries = []
3232
self.groups = {}
3333
self.sitemaps = []
3434
self.default_entry = None
3535
self.disallow_all = False
3636
self.allow_all = False
37-
self.set_url(url)
37+
self.set_url(url_or_request)
3838
self.last_checked = 0
3939

4040
def mtime(self):
@@ -54,15 +54,17 @@ def modified(self):
5454
import time
5555
self.last_checked = time.time()
5656

57-
def set_url(self, url):
58-
"""Sets the URL referring to a robots.txt file."""
59-
self.url = url
57+
def set_url(self, url_or_request):
58+
"""Sets the URL referring to a robots.txt file.
59+
url_or_request can be a string or a Request object.
60+
"""
61+
self.url = url_or_request
6062

61-
if isinstance(url, urllib.request.Request):
62-
self.host = url.host
63-
self.path = url.selector
63+
if isinstance(url_or_request, urllib.request.Request):
64+
self.host = url_or_request.host
65+
self.path = url_or_request.selector
6466
else:
65-
self.host, self.path = urllib.parse.urlparse(url)[1:3]
67+
self.host, self.path = urllib.parse.urlparse(url_or_request)[1:3]
6668

6769
def read(self):
6870
"""Reads the robots.txt URL and feeds it to the parser."""

0 commit comments

Comments
 (0)