Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 1 | """ robotparser.py |
Guido van Rossum | bbf8c2f | 1997-01-30 03:18:23 +0000 | [diff] [blame] | 2 | |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 3 | Copyright (C) 2000 Bastian Kleineidam |
Guido van Rossum | bbf8c2f | 1997-01-30 03:18:23 +0000 | [diff] [blame] | 4 | |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 5 | You can choose between two licenses when using this package: |
| 6 | 1) GNU GPLv2 |
| 7 | 2) PYTHON 2.0 OPEN SOURCE LICENSE |
| 8 | |
| 9 | The robots.txt Exclusion Protocol is implemented as specified in |
| 10 | http://info.webcrawler.com/mak/projects/robots/norobots-rfc.html |
Guido van Rossum | bbf8c2f | 1997-01-30 03:18:23 +0000 | [diff] [blame] | 11 | """ |
Eric S. Raymond | 141971f | 2001-02-09 08:40:40 +0000 | [diff] [blame] | 12 | import re,urlparse,urllib |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 13 | |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 14 | __all__ = ["RobotFileParser"] |
| 15 | |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 16 | debug = 0 |
| 17 | |
| 18 | def _debug(msg): |
| 19 | if debug: print msg |
| 20 | |
Guido van Rossum | bbf8c2f | 1997-01-30 03:18:23 +0000 | [diff] [blame] | 21 | |
| 22 | class RobotFileParser: |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 23 | def __init__(self, url=''): |
| 24 | self.entries = [] |
| 25 | self.disallow_all = 0 |
| 26 | self.allow_all = 0 |
| 27 | self.set_url(url) |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 28 | self.last_checked = 0 |
Guido van Rossum | bbf8c2f | 1997-01-30 03:18:23 +0000 | [diff] [blame] | 29 | |
| 30 | def mtime(self): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 31 | return self.last_checked |
Guido van Rossum | bbf8c2f | 1997-01-30 03:18:23 +0000 | [diff] [blame] | 32 | |
| 33 | def modified(self): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 34 | import time |
| 35 | self.last_checked = time.time() |
Guido van Rossum | bbf8c2f | 1997-01-30 03:18:23 +0000 | [diff] [blame] | 36 | |
| 37 | def set_url(self, url): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 38 | self.url = url |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 39 | self.host, self.path = urlparse.urlparse(url)[1:3] |
Guido van Rossum | bbf8c2f | 1997-01-30 03:18:23 +0000 | [diff] [blame] | 40 | |
| 41 | def read(self): |
Skip Montanaro | 5bba231 | 2001-02-12 20:58:30 +0000 | [diff] [blame] | 42 | opener = URLopener() |
| 43 | f = opener.open(self.url) |
| 44 | lines = f.readlines() |
| 45 | self.errcode = opener.errcode |
| 46 | if self.errcode == 401 or self.errcode == 403: |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 47 | self.disallow_all = 1 |
Skip Montanaro | 5bba231 | 2001-02-12 20:58:30 +0000 | [diff] [blame] | 48 | _debug("disallow all") |
| 49 | elif self.errcode >= 400: |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 50 | self.allow_all = 1 |
Skip Montanaro | 5bba231 | 2001-02-12 20:58:30 +0000 | [diff] [blame] | 51 | _debug("allow all") |
| 52 | elif self.errcode == 200 and lines: |
| 53 | _debug("parse lines") |
| 54 | self.parse(lines) |
Guido van Rossum | bbf8c2f | 1997-01-30 03:18:23 +0000 | [diff] [blame] | 55 | |
| 56 | def parse(self, lines): |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 57 | """parse the input lines from a robot.txt file. |
Tim Peters | dfc538a | 2001-01-21 04:49:16 +0000 | [diff] [blame] | 58 | We allow that a user-agent: line is not preceded by |
| 59 | one or more blank lines.""" |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 60 | state = 0 |
| 61 | linenumber = 0 |
| 62 | entry = Entry() |
Tim Peters | dfc538a | 2001-01-21 04:49:16 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 64 | for line in lines: |
Eric S. Raymond | 141971f | 2001-02-09 08:40:40 +0000 | [diff] [blame] | 65 | line = line.strip() |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 66 | linenumber = linenumber + 1 |
| 67 | if not line: |
| 68 | if state==1: |
| 69 | _debug("line %d: warning: you should insert" |
Tim Peters | dfc538a | 2001-01-21 04:49:16 +0000 | [diff] [blame] | 70 | " allow: or disallow: directives below any" |
| 71 | " user-agent: line" % linenumber) |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 72 | entry = Entry() |
| 73 | state = 0 |
| 74 | elif state==2: |
| 75 | self.entries.append(entry) |
| 76 | entry = Entry() |
| 77 | state = 0 |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 78 | # remove optional comment and strip line |
Eric S. Raymond | 141971f | 2001-02-09 08:40:40 +0000 | [diff] [blame] | 79 | i = line.find('#') |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 80 | if i>=0: |
| 81 | line = line[:i] |
Eric S. Raymond | 141971f | 2001-02-09 08:40:40 +0000 | [diff] [blame] | 82 | line = line.strip() |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 83 | if not line: |
| 84 | continue |
Eric S. Raymond | 141971f | 2001-02-09 08:40:40 +0000 | [diff] [blame] | 85 | line = line.split(':', 1) |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 86 | if len(line) == 2: |
Eric S. Raymond | 141971f | 2001-02-09 08:40:40 +0000 | [diff] [blame] | 87 | line[0] = line[0].strip().lower() |
| 88 | line[1] = line[1].strip() |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 89 | if line[0] == "user-agent": |
| 90 | if state==2: |
| 91 | _debug("line %d: warning: you should insert a blank" |
Tim Peters | dfc538a | 2001-01-21 04:49:16 +0000 | [diff] [blame] | 92 | " line before any user-agent" |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 93 | " directive" % linenumber) |
| 94 | self.entries.append(entry) |
| 95 | entry = Entry() |
| 96 | entry.useragents.append(line[1]) |
| 97 | state = 1 |
| 98 | elif line[0] == "disallow": |
| 99 | if state==0: |
| 100 | _debug("line %d: error: you must insert a user-agent:" |
Tim Peters | dfc538a | 2001-01-21 04:49:16 +0000 | [diff] [blame] | 101 | " directive before this line" % linenumber) |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 102 | else: |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 103 | entry.rulelines.append(RuleLine(line[1], 0)) |
| 104 | state = 2 |
| 105 | elif line[0] == "allow": |
| 106 | if state==0: |
| 107 | _debug("line %d: error: you must insert a user-agent:" |
Tim Peters | dfc538a | 2001-01-21 04:49:16 +0000 | [diff] [blame] | 108 | " directive before this line" % linenumber) |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 109 | else: |
| 110 | entry.rulelines.append(RuleLine(line[1], 1)) |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 111 | else: |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 112 | _debug("line %d: warning: unknown key %s" % (linenumber, |
| 113 | line[0])) |
| 114 | else: |
| 115 | _debug("line %d: error: malformed line %s"%(linenumber, line)) |
| 116 | if state==2: |
| 117 | self.entries.append(entry) |
| 118 | _debug("Parsed rules:\n%s" % str(self)) |
Guido van Rossum | bbf8c2f | 1997-01-30 03:18:23 +0000 | [diff] [blame] | 119 | |
Guido van Rossum | bbf8c2f | 1997-01-30 03:18:23 +0000 | [diff] [blame] | 120 | |
Guido van Rossum | dc8b798 | 2000-03-27 19:29:31 +0000 | [diff] [blame] | 121 | def can_fetch(self, useragent, url): |
| 122 | """using the parsed robots.txt decide if useragent can fetch url""" |
Skip Montanaro | 5bba231 | 2001-02-12 20:58:30 +0000 | [diff] [blame] | 123 | _debug("Checking robot.txt allowance for:\n user agent: %s\n url: %s" % |
| 124 | (useragent, url)) |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 125 | if self.disallow_all: |
| 126 | return 0 |
| 127 | if self.allow_all: |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 128 | return 1 |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 129 | # search for given user agent matches |
| 130 | # the first match counts |
Skip Montanaro | 5bba231 | 2001-02-12 20:58:30 +0000 | [diff] [blame] | 131 | url = urllib.quote(urlparse.urlparse(url)[2]) or "/" |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 132 | for entry in self.entries: |
| 133 | if entry.applies_to(useragent): |
| 134 | return entry.allowance(url) |
| 135 | # agent not found ==> access granted |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 136 | return 1 |
Guido van Rossum | bbf8c2f | 1997-01-30 03:18:23 +0000 | [diff] [blame] | 137 | |
Guido van Rossum | bbf8c2f | 1997-01-30 03:18:23 +0000 | [diff] [blame] | 138 | |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 139 | def __str__(self): |
| 140 | ret = "" |
| 141 | for entry in self.entries: |
| 142 | ret = ret + str(entry) + "\n" |
| 143 | return ret |
| 144 | |
| 145 | |
| 146 | class RuleLine: |
| 147 | """A rule line is a single "Allow:" (allowance==1) or "Disallow:" |
| 148 | (allowance==0) followed by a path.""" |
| 149 | def __init__(self, path, allowance): |
| 150 | self.path = urllib.quote(path) |
| 151 | self.allowance = allowance |
| 152 | |
| 153 | def applies_to(self, filename): |
| 154 | return self.path=="*" or re.match(self.path, filename) |
| 155 | |
| 156 | def __str__(self): |
| 157 | return (self.allowance and "Allow" or "Disallow")+": "+self.path |
| 158 | |
| 159 | |
| 160 | class Entry: |
| 161 | """An entry has one or more user-agents and zero or more rulelines""" |
| 162 | def __init__(self): |
| 163 | self.useragents = [] |
| 164 | self.rulelines = [] |
| 165 | |
| 166 | def __str__(self): |
| 167 | ret = "" |
| 168 | for agent in self.useragents: |
| 169 | ret = ret + "User-agent: "+agent+"\n" |
| 170 | for line in self.rulelines: |
| 171 | ret = ret + str(line) + "\n" |
| 172 | return ret |
| 173 | |
| 174 | def applies_to(self, useragent): |
Skip Montanaro | 5bba231 | 2001-02-12 20:58:30 +0000 | [diff] [blame] | 175 | """check if this entry applies to the specified agent""" |
| 176 | # split the name token and make it lower case |
| 177 | useragent = useragent.split("/")[0].lower() |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 178 | for agent in self.useragents: |
Skip Montanaro | 5bba231 | 2001-02-12 20:58:30 +0000 | [diff] [blame] | 179 | if agent=='*': |
| 180 | # we have the catch-all agent |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 181 | return 1 |
Skip Montanaro | 5bba231 | 2001-02-12 20:58:30 +0000 | [diff] [blame] | 182 | agent = agent.lower() |
| 183 | # don't forget to re.escape |
| 184 | if re.search(re.escape(useragent), agent): |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 185 | return 1 |
| 186 | return 0 |
| 187 | |
| 188 | def allowance(self, filename): |
| 189 | """Preconditions: |
| 190 | - our agent applies to this entry |
| 191 | - filename is URL decoded""" |
| 192 | for line in self.rulelines: |
Skip Montanaro | 5bba231 | 2001-02-12 20:58:30 +0000 | [diff] [blame] | 193 | _debug((filename, str(line), line.allowance)) |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 194 | if line.applies_to(filename): |
| 195 | return line.allowance |
| 196 | return 1 |
| 197 | |
Skip Montanaro | 5bba231 | 2001-02-12 20:58:30 +0000 | [diff] [blame] | 198 | class URLopener(urllib.FancyURLopener): |
| 199 | def __init__(self, *args): |
| 200 | apply(urllib.FancyURLopener.__init__, (self,) + args) |
| 201 | self.errcode = 200 |
| 202 | self.tries = 0 |
| 203 | self.maxtries = 10 |
Tim Peters | 0e6d213 | 2001-02-15 23:56:39 +0000 | [diff] [blame] | 204 | |
Skip Montanaro | 5bba231 | 2001-02-12 20:58:30 +0000 | [diff] [blame] | 205 | def http_error_default(self, url, fp, errcode, errmsg, headers): |
| 206 | self.errcode = errcode |
| 207 | return urllib.FancyURLopener.http_error_default(self, url, fp, errcode, |
| 208 | errmsg, headers) |
| 209 | |
| 210 | def http_error_302(self, url, fp, errcode, errmsg, headers, data=None): |
| 211 | self.tries += 1 |
| 212 | if self.tries >= self.maxtries: |
| 213 | return self.http_error_default(url, fp, 500, |
| 214 | "Internal Server Error: Redirect Recursion", |
| 215 | headers) |
| 216 | result = urllib.FancyURLopener.http_error_302(self, url, fp, errcode, |
| 217 | errmsg, headers, data) |
| 218 | self.tries = 0 |
| 219 | return result |
| 220 | |
| 221 | def _check(a,b): |
| 222 | if not b: |
| 223 | ac = "access denied" |
| 224 | else: |
| 225 | ac = "access allowed" |
| 226 | if a!=b: |
| 227 | print "failed" |
| 228 | else: |
| 229 | print "ok (%s)" % ac |
| 230 | print |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 231 | |
| 232 | def _test(): |
| 233 | global debug |
| 234 | import sys |
| 235 | rp = RobotFileParser() |
| 236 | debug = 1 |
Skip Montanaro | 5bba231 | 2001-02-12 20:58:30 +0000 | [diff] [blame] | 237 | |
| 238 | # robots.txt that exists, gotten to by redirection |
| 239 | rp.set_url('http://www.musi-cal.com/robots.txt') |
| 240 | rp.read() |
| 241 | |
| 242 | # test for re.escape |
| 243 | _check(rp.can_fetch('*', 'http://www.musi-cal.com/'), 1) |
| 244 | # this should match the first rule, which is a disallow |
| 245 | _check(rp.can_fetch('', 'http://www.musi-cal.com/'), 0) |
| 246 | # various cherry pickers |
| 247 | _check(rp.can_fetch('CherryPickerSE', |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 248 | 'http://www.musi-cal.com/cgi-bin/event-search' |
Skip Montanaro | 5bba231 | 2001-02-12 20:58:30 +0000 | [diff] [blame] | 249 | '?city=San+Francisco'), 0) |
| 250 | _check(rp.can_fetch('CherryPickerSE/1.0', |
| 251 | 'http://www.musi-cal.com/cgi-bin/event-search' |
| 252 | '?city=San+Francisco'), 0) |
| 253 | _check(rp.can_fetch('CherryPickerSE/1.5', |
| 254 | 'http://www.musi-cal.com/cgi-bin/event-search' |
| 255 | '?city=San+Francisco'), 0) |
| 256 | # case sensitivity |
| 257 | _check(rp.can_fetch('ExtractorPro', 'http://www.musi-cal.com/blubba'), 0) |
| 258 | _check(rp.can_fetch('extractorpro', 'http://www.musi-cal.com/blubba'), 0) |
| 259 | # substring test |
| 260 | _check(rp.can_fetch('toolpak/1.1', 'http://www.musi-cal.com/blubba'), 0) |
| 261 | # tests for catch-all * agent |
| 262 | _check(rp.can_fetch('spam', 'http://www.musi-cal.com/search'), 0) |
| 263 | _check(rp.can_fetch('spam', 'http://www.musi-cal.com/Musician/me'), 1) |
| 264 | _check(rp.can_fetch('spam', 'http://www.musi-cal.com/'), 1) |
| 265 | _check(rp.can_fetch('spam', 'http://www.musi-cal.com/'), 1) |
| 266 | |
| 267 | # robots.txt that does not exist |
| 268 | rp.set_url('http://www.lycos.com/robots.txt') |
| 269 | rp.read() |
| 270 | _check(rp.can_fetch('Mozilla', 'http://www.lycos.com/search'), 1) |
Skip Montanaro | 663f6c2 | 2001-01-20 15:59:25 +0000 | [diff] [blame] | 271 | |
| 272 | if __name__ == '__main__': |
Guido van Rossum | dc8b798 | 2000-03-27 19:29:31 +0000 | [diff] [blame] | 273 | _test() |