1. 程式人生 > 實用技巧 >robotparser (File Formats) – Python 中文開發手冊

robotparser (File Formats) – Python 中文開發手冊

[
  •   Python 中文開發手冊

    robotparser (File Formats) - Python 中文開發手冊

    注意

    robotparser模組已urllib.robotparser在Python 3中重新命名。當將原始碼轉換為Python 3時,2to3工具將自動適應匯入。

    該模組提供了一個類,RobotFileParser它回答關於特定使用者代理是否可以在釋出該robots.txt檔案的網站上獲取URL的問題。有關robots.txt檔案結構的更多詳細資訊,請參閱http://www.robotstxt.org/orig.html。

    class robotparser.RobotFileParser(url='')

    這個類提供了一些方法來讀取,解析和回答有關urlrobots.txt檔案的問題。

    set_url(url)

    設定引用robots.txt檔案的URL 。

    read()

    讀取robots.txtURL並將其提供給解析器。

    parse(lines)

    解析行引數。

    can_fetch(useragent, url)

    返回True是否允許useragent根據解析檔案中包含的規則獲取urlrobots.txt。

    mtime()

    返回robots.txt檔案上次獲取的時間。這對於需要robots.txt定期檢查新檔案的長時間執行的網路蜘蛛非常有用。

    modified()

    設定robots.txt檔案上次獲取到當前時間的時間。

    以下示例演示了RobotFileParser類的基本用法。

    >>> import robotparser
    >>> rp = robotparser.RobotFileParser()
    >>> rp.set_url("https://www.musi-cal.com/robots.txt")
    >>> rp.read()
    >>> rp.can_fetch("*", "http://www.musi-cal.com/cgi-bin/search?city=San+Francisco")
    False
    >>> rp.can_fetch("*", "http://www.musi-cal.com/")
    True
  •   Python 中文開發手冊
    ]
  •   本文標題:robotparser (File Formats) – Python 中文開發手冊 - Break易站轉載請保留頁面地址:https://www.breakyizhan.com/python/35187.html