2010-01-31 69 views
1

我需要檢索有關ventrilo服務器狀態的信息。 ventrilo的網站提供檢索服務器信息的服務。例如,這是遊戲競技場ventrilo服務器:http://www.ventrilo.com/status.php?hostname=144.140.154.11&port=25000用jquery檢索外部頁面數據

我試圖抓住「頻道和用戶信息」部分。但該表沒有標識,頁面上還有許多其他表。我該如何去做這件事?

我還聽說有些瀏覽器不允許您加載外部內容。我怎麼能解決這個問題?

+0

您有權訪問服務器端腳本語言,還是想在客戶端執行此操作? – brianpeiris 2010-01-31 09:03:33

回答

3

除了馬克的回答,您可以使用雅虎的YQL服務與下面的查詢檢索表

select * from html where url="http://www.ventrilo.com/status.php?hostname=144.140.154.11&port=25000" and xpath='//center[4]/table' 

然後,您可以使用與jQuery的YQL結果顯示在顯示在你的網站上的表格下面的演示。

工作演示

http://jsbin.com/eveka(編輯通過http://jsbin.com/eveka/edit

全部來源

的JavaScript

var 
    ventriloStatus = $('#ventriloStatus'), 
    hostname = '144.140.154.11', 
    port = 25000, 
    ventriloURL = 'http://www.ventrilo.com/status.php?hostname=' + hostname + '&port=' + port, 
    yqlURL = 'http://query.yahooapis.com/v1/public/yql?callback=?', 
    xpath = '//center[4]/table', 
    query = 'select * from html where url="' + ventriloURL + '" and xpath="' + xpath + '"' 
    data = { 
    q: query, 
    format: 'xml', 
    diagnostics: false 
    }; 

ventriloStatus.text('Loading...'); 
$.getJSON(yqlURL, data, function (response) { 
    ventriloStatus.html(response.results[0]).find('img').each(function() { 
    this.src = 'http://www.ventrilo.com/' + this.src.split('/').slice(-1)[0]; 
    }); 
}); 

HTML

<!DOCTYPE html> 
<html> 
<head> 
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script> 
<meta charset=utf-8 /> 
<title>Ventrilo with YQL</title> 
<style> 
    body { font: 8pt sans-serif; } 
    p { display: inline; } 
    #ventriloStatus{ width: 600px; } 
</style> 
</head> 
<body> 
    <h1>Ventrilo Status</h1> 
    <div id="ventriloStatus"></div> 
</body> 
</html> 
1

兩種方式:

1)如果有一個JSON的服務,您可以使用JSONP(檢查谷歌,但我個人不喜歡這個)

2)如果您的服務器端使用PHP,取看看PHPQuery,在腳本中執行服務器端的抓取操作,然後在AJAX中調用該腳本來獲取提取的數據(您可能希望實現某種級別的緩存,因爲您使用的是網關... APC將是友好的工具)。