好的網絡爬蟲,首先須要遵照Robots協議。Robots協議(也稱爲爬蟲協議、機器人協議等)的全稱是「網絡爬蟲排除標準」(Robots Exclusion Protocol),網站經過Robots協議告訴搜索引擎哪些頁面能夠抓取,哪些頁面不能抓取。
在網站根目錄下放一個robots.txt文本文件(如 https://www.taobao.com/robots... ),裏面能夠指定不一樣的網絡爬蟲能訪問的頁面和禁止訪問的頁面,指定的頁面由正則表達式表示。網絡爬蟲在採集這個網站以前,首先獲取到這個robots.txt文本文件,而後解析到其中的規則,而後根據規則來採集網站的數據。html
注意: 一個英文要大寫,冒號是英文狀態下,冒號後面有一個空格,"/"表明整個網站正則表達式
禁止全部機器人訪問 User-agent: * Disallow: / 容許全部機器人訪問 User-agent: * Disallow: 禁止特定機器人訪問 User-agent: BadBot Disallow: / 容許特定機器人訪問 User-agent: GoodBot Disallow: 禁止訪問特定目錄 User-agent: * Disallow: /images/ 僅容許訪問特定目錄 User-agent: * Allow: /images/ Disallow: / 禁止訪問特定文件 User-agent: * Disallow: /*.html$ 僅容許訪問特定文件 User-agent: * Allow: /*.html$ Disallow: /