2016-01-01 22 views
3

我有這個令人敬畏的工作代碼,從維基百科拉動自動填充建議。這非常有用,除了我希望它只推薦樂隊。舉例來說,輸入「Pink」只會返回以「Pink」開頭的/ bands /,而不是世界上處理「Pink」的所有內容。如何將Wikipedia API自動填充建議限制到某個類別?

有誰知道如何做到這一點? http://codepen.io/anon/pen/VePapK

我發現有一種方法,通過查詢動作here(而不是OpenSearch的)來過濾按類別的結果,但奇怪的是,似乎沒有成爲一個廣泛的「音樂」或「帶」類別。所以這是開放更多未回答的問題....任何幫助表示讚賞。

編輯:另外,如果有人知道一個更簡單的方法來更新列表中的所有重要樂隊在您的網站的下拉列表,請讓我知道。

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
</head> 

<body> 
      <form method="get" id="search"> 
       <input type="text" class="searchbox" value="Type here.. " onblur="if(this.value == '') { this.value = 'Type here..'; }" onfocus="if(this.value == 'Type here..') { this.value = ''; }" name="s"> 
       <button type="submit">Submit</button> 
      </form> 





<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/themes/smoothness/jquery-ui.css" /> 
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js"></script> 
<script type="text/javascript"> 
$(".searchbox").autocomplete({ 
    source: function(request, response) { 
     console.log(request.term); 
     $.ajax({ 
      url: "http://en.wikipedia.org/w/api.php", 
      dataType: "jsonp", 
      data: { 
       'action': "opensearch", 
       'format': "json", 
       'search': request.term 
      }, 
      success: function(data) { 
       response(data[1]); 
      } 
     }); 
    } 
}); 
</script> 
</body> 
</html> 

回答

4

確實有很多問題,打開維基百科的分類系統。謝天謝地,還有更好的辦法。

這是一種替代方法,將讓你從維基百科搜索需要的東西:你可以使用維基百科的CirrusSearch API搜索參數的類別中獲得物品(經incategory:<category title>),並鏈接到另一個維基百科頁面(通過linksto:<page name>)或者通過跨越常見Wikipedia模板的頁面(通過hastemplate:<template name>)。如果您知道正確的Wikipedia類別或頁面,則可以輕鬆使用前兩個選項。雖然最後一個聽起來很奇怪,但維基百科的組織良好的模板系統可能是您最好的選擇。許多樂隊的文章將有一個共同的infobox on the right,這將幫助你縮小搜索範圍。

您可以使用CirrusSearch和hastemplate:Infobox_musical_artist在一起,就像這樣:

https://en.wikipedia.org/w/api.php?action=query&list=search&format=json&srsearch=hastemplate%3AInfobox_musical_artist+daft

它返回此格式頁面的列表:

{ 
"batchcomplete": "", 
"continue": { 
    "sroffset": 10, 
    "continue": "-||" 
}, 
"query": { 
    "searchinfo": { 
     "totalhits": 470, 
     "suggestion": "hastemplate:Infobox_musical_artist dart", 
     "suggestionsnippet": "hastemplate:Infobox_musical_artist <em>dart</em>" 
    }, 
    "search": [ 
     { 
      "ns": 0, 
      "title": "Daft Punk", 
      "snippet": "<span class=\"searchmatch\">Daft</span> Punk are an electronic music duo consisting of French musicians Guy-Manuel de Homem-Christo and Thomas Bangalter. The duo achieved significant popularity", 
      "size": 76278, 
      "wordcount": 8170, 
      "timestamp": "2016-02-08T01:33:54Z" 
     }, 
     ... 

這裏是一個CodePen演示http://codepen.io/slaporte/pen/obmaWY

它仍然可能無法捕獲維基百科上有100%的樂隊。如果您正在尋找特定的子設備,您可以嘗試使用參數hastemplate以及鏈接或類別來縮小或擴大您的搜索範圍。您可以瞭解更多關於其他維基百科CirrusSearch參數here

順便說一句,這是一個有趣的自動完成的方法!

相關問題