網站常常會被各類爬蟲光顧,有的是搜索引擎爬蟲,有的不是,一般狀況下這些爬蟲都有UserAgent,而咱們知道UserAgent是能夠假裝的,UserAgent的本質是Http請求頭中的一個選項設置,經過編程的方式能夠給請求設置任意的UserAgent。 python
因此經過UserAgent判斷請求的發起者是不是搜索引擎爬蟲(蜘蛛)的方式是不靠譜的,更靠譜的方法是經過請求者的ip對應的host主機名是不是搜索引擎本身家的host的方式來判斷。linux
要得到ip的host,在windows下能夠經過nslookup命令,在linux下能夠經過host命令來得到,例如:web
這裏我在windows下執行了nslookup ip 的命令,從上圖能夠看到這個ip的主機名是crawl-66-249-64-119.googlebot.com。 這說明這個ip是一個google爬蟲,google爬蟲的域名都是 xxx.googlebot.com.編程
咱們也能夠經過python程序的方式來得到ip的host信息,代碼以下:c#
import socket def getHost(ip): try: result=socket.gethostbyaddr(ip) if result: return result[0], None except socket.herror,e: return None, e.message
上述代碼使用了socket模塊的gethostbyaddr的方法得到ip地址的主機名。windows
經常使用蜘蛛的域名都和搜索引擎官網的域名相關,例如:socket
基於以上原理,我寫了一個工具頁面提供判斷ip是不是真實搜索引擎的工具頁面,該頁面上提供了網頁判斷的工具和常見的google和bing的搜索引擎爬蟲的ip地址。ide
頁面地址:http://outofmemory.cn/tools/is-search-engine-spider-ip/ 工具
本文提供的代碼是python代碼,經過c#代碼也是能夠實現的,原理是同樣的。網站
附帶常見搜索引擎蜘蛛的IP段:
蜘蛛名稱 | IP地址 |
---|---|
Baiduspider | 202.108.11.* 220.181.32.* 58.51.95.* 60.28.22.* 61.135.162.* 61.135.163.* 61.135.168.* |
YodaoBot | 202.108.7.215 202.108.7.220 202.108.7.221 |
Sogou web spider | 219.234.81.* 220.181.61.* |
Googlebot | 203.208.60.* |
Yahoo! Slurp | 202.160.181.* 72.30.215.* 74.6.17.* 74.6.22.* |
Yahoo ContentMatch Crawler | 119.42.226.* 119.42.230.* |
Sogou-Test-Spider | 220.181.19.103 220.181.26.122 |
Twiceler | 38.99.44.104 64.34.251.9 |
Yahoo! Slurp China | 202.160.178.* |
Sosospider | 124.115.0.* |
CollapsarWEB qihoobot | 221.194.136.18 |
NaverBot | 202.179.180.45 |
Sogou Orion spider | 220.181.19.106 220.181.19.74 |
Sogou head spider | 220.181.19.107 |
SurveyBot | 216.145.5.42 64.246.165.160 |
Yanga WorldSearch Bot v | 77.91.224.19 91.205.124.19 |
baiduspider-mobile-gate | 220.181.5.34 61.135.166.31 |
discobot | 208.96.54.70 |
ia_archiver | 209.234.171.42 |
msnbot | 65.55.104.209 65.55.209.86 65.55.209.96 |
sogou in spider | 220.181.19.216 |