urllib.robotparser --- robots.txt 的剖析器

原始碼:Lib/urllib/robotparser.py


This module provides a single class, RobotFileParser, which answers questions about whether or not a particular user agent can fetch a URL on the website that published the robots.txt file. For more details on the structure of robots.txt files, see http://www.robotstxt.org/orig.html.

class urllib.robotparser.RobotFileParser(url='')

此類別提供了一些方法可以讀取、剖析和回答關於 url 上的 robots.txt 文件的問題。

set_url(url)

設置指向 robots.txt 文件的 URL。

read()

讀取 robots.txt URL 並將其輸入到剖析器。

parse(lines)

剖析 lines 引數。

can_fetch(useragent, url)

根據從 robots.txt 文件中剖析出的規則,如果 useragent 被允許 fetch url 的話,則回傳 True

mtime()

回傳最近一次 fetch robots.txt 文件的時間。這適用於需要定期檢查 robots.txt 文件更新情況的長時間運行網頁爬蟲。

modified()

將最近一次 fetch robots.txt 文件的時間設置為目前時間。

crawl_delay(useragent)

針對指定的 useragentrobots.txt 回傳 Crawl-delay 參數的值。如果此參數不存在、不適用於指定的 useragent ,或是此參數在 robots.txt 中所指的條目含有無效語法,則回傳 None

在 3.6 版被加入.

request_rate(useragent)

named tuple RequestRate(requests, seconds) 的形式從 robots.txt 回傳 Request-rate 參數的內容。如果此參數不存在、不適用於指定的 useragent ,或是此參數在 robots.txt 中所指的條目含有無效語法,則回傳 None

在 3.6 版被加入.

site_maps()

list() 的形式從 robots.txt 回傳 Sitemap 參數的內容。如果此參數不存在或此參數在 robots.txt 中所指的條目含有無效語法,則回傳 None

在 3.8 版被加入.

下面的範例展示了 RobotFileParser 類別的基本用法:

>>> import urllib.robotparser
>>> rp = urllib.robotparser.RobotFileParser()
>>> rp.set_url("http://www.musi-cal.com/robots.txt")
>>> rp.read()
>>> rrate = rp.request_rate("*")
>>> rrate.requests
3
>>> rrate.seconds
20
>>> rp.crawl_delay("*")
6
>>> rp.can_fetch("*", "http://www.musi-cal.com/cgi-bin/search?city=San+Francisco")
False
>>> rp.can_fetch("*", "http://www.musi-cal.com/")
True