<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 3.1.4 分析Robots協議 ## 1.說明 利用 Urllib 的 robotparser 模塊我們可以實現網站 Robots 協議的分析 ## 2\. Robots協議 {#1-robots協議} Robots 協議也被稱作爬蟲協議、機器人協議,它的全名叫做網絡爬蟲排除標準(Robots Exclusion Protocol),用來告訴爬蟲和搜索引擎哪些頁面可以抓取,哪些不可以抓取。它通常是一個叫做 robots.txt 的文本文件,放在網站的根目錄下。 當搜索爬蟲訪問一個站點時,它首先會檢查下這個站點根目錄下是否存在 robots.txt 文件,如果存在,搜索爬蟲會根據其中定義的爬取范圍來爬取。如果沒有找到這個文件,那么搜索爬蟲便會訪問所有可直接訪問的頁面。 例如: ~~~text https://blog.csdn.net/robots.txt ~~~ robots.txt內容: ~~~text User-agent: * Disallow: /css/ Disallow: /images/ Disallow: /content/ Disallow: /ui/ Disallow: /js/ Sitemap: https://blog.csdn.net/s/sitemap/pcsitemapindex.xml ~~~ User-agent 描述了搜索爬蟲的名稱,在這里將值設置為 \*,則代表該協議對任何的爬取爬蟲有效 Disallow 指定了不允許抓取的目錄,比如上述例子中設置為/則代表不允許抓取所有頁面。 Allow 一般和 Disallow 一起使用,一般不會單獨使用,用來排除某些限制,現在我們設置為 /public/ ,起到的作用是所有頁面不允許抓取,但是 public 目錄是可以抓取的 ## 3\. 爬蟲名稱 {#2-爬蟲名稱} 大家可能會疑惑,爬蟲名是哪兒來的?為什么就叫這個名?其實它是有固定名字的了,比如百度的就叫做 BaiduSpider,下面的表格列出了一些常見的搜索爬蟲的名稱及對應的網站: | 爬蟲名稱 | 名稱 | 網站 | | --- | --- | --- | | BaiduSpider | 百度 | [www.baidu.com](http://www.baidu.com/) | | Googlebot | 谷歌 | [www.google.com](http://www.google.com/) | | 360Spider | 360搜索 | [www.so.com](http://www.so.com/) | | YodaoBot | 有道 | [www.youdao.com](http://www.youdao.com/) | | ia\_archiver | Alexa | [www.alexa.cn](http://www.alexa.cn/) | | Scooter | altavista | [www.altavista.com](http://www.altavista.com/) | ## 4\. robotparser {#3-robotparser} 了解了什么是 Robots 協議之后,我們就可以使用 robotparser 模塊來解析 robots.txt 了。 robotparser 模塊提供了一個類,叫做 RobotFileParser。它可以根據某網站的 robots.txt 文件來判斷一個爬取爬蟲是否有權限來爬取這個網頁。 語法聲明: ~~~text urllib.robotparser.RobotFileParser(url='') ~~~ 使用這個類的時候非常簡單,只需要在構造方法里傳入 robots.txt的鏈接即可。當然也可以聲明時不傳入,默認為空,再使用 set\_url() 方法設置一下也可以。 有常用的幾個方法分別介紹一下: * set\_url(),用來設置 robots.txt 文件的鏈接。如果已經在創建 RobotFileParser 對象時傳入了鏈接,那就不需要再使用這個方法設置了。 * read(),讀取 robots.txt 文件并進行分析,注意這個函數是執行一個讀取和分析操作,如果不調用這個方法,接下來的判斷都會為 False,所以一定記得調用這個方法,這個方法不會返回任何內容,但是執行了讀取操作。 * parse(),用來解析 robots.txt 文件,傳入的參數是 robots.txt 某些行的內容,它會按照 robots.txt 的語法規則來分析這些內容。 * can\_fetch(),方法傳入兩個參數,第一個是 User-agent,第二個是要抓取的 URL,返回的內容是該搜索引擎是否可以抓取這個 URL,返回結果是 True 或 False。 * mtime(),返回的是上次抓取和分析 robots.txt 的時間,這個對于長時間分析和抓取的搜索爬蟲是很有必要的,你可能需要定期檢查來抓取最新的 robots.txt。 * modified(),同樣的對于長時間分析和抓取的搜索爬蟲很有幫助,將當前時間設置為上次抓取和分析 robots.txt 的時間。 實例: ~~~text from urllib.robotparser import RobotFileParser rp = RobotFileParser() rp.set_url('http://www.jianshu.com/robots.txt') rp.read() print(rp.can_fetch('*', 'http://www.jianshu.com/p/b67554025d7d')) print(rp.can_fetch('*', "http://www.jianshu.com/search?q=python&page=1&type=collections")) ~~~ 以簡書為例,我們首先創建 RobotFileParser 對象,然后通過 set\_url() 方法來設置了 robots.txt 的鏈接。當然不用這個方法的話,可以在聲明時直接用如下方法設置: ~~~text rp = RobotFileParser('http://www.jianshu.com/robots.txt') ~~~ 利用 can\_fetch() 方法來判斷了網頁是否可以被抓取 運行結果: ~~~text False False ~~~ 可以使用 parser() 方法執行讀取和分析 實例: ~~~text from urllib.robotparser import RobotFileParser from urllib.request import urlopen rp = RobotFileParser() rp.parse(urlopen('https://blog.csdn.net/robots.txt').read().decode('utf-8').split('\n')) print(rp.can_fetch('*', 's://blog.csdn.net/Linear_Luo/article/details/52231550')) ~~~ 運行結果: ~~~text True ~~~ tips:一般都不會用這個
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看