调用百度“搜索建议(BaiduSuggestion)”的 API
生活随笔
收集整理的這篇文章主要介紹了
调用百度“搜索建议(BaiduSuggestion)”的 API
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原理
當前發送一個 Get 請求:http://suggestion.baidu.com/su?wd=wq&p=3&cb=window.bdsug.sug&t=1324271669786
注意 wd:搜索關鍵字
cb:callback 回調函數
那么百度將返回一個如下的 json 數據:
window.bdsug.sug({q:"wq",p:true,s:["wqvga","wqi","wqsg_umd","wqynyl","wqi下載","wqrc","wqnmlgb","wqvga分辨率","吳奇隆","吾棲之膚"]});
?
其實最復雜的是如何在客戶端展示這些數據,需要用到 css 和 js。
正好園子里有朋友做了,感謝!
http://www.cnblogs.com/sufei/archive/2011/12/19/2293289.html
主要代碼:
index.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>調用百度的“搜索建議”</title>
<link href="js/StyleSheet.css" rel="stylesheet" type="text/css" />
<script src="js/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="js/JScript2.js" type="text/javascript"></script>
</head>
<body>
<input style="width: 500px;" url="true" id="Text1" type="text" />
<br />
<input style="width: 500px;" id="Text2" type="text" />
<div style="display: none; position: absolute;" id="allSitesBoxHdl" class="classlist"
onmouseover="this.style.display='block'" onmouseout="this.style.display='none'">
<ul id="allSitesBoxContent">
</ul>
</div>
<script type="text/javascript">
Init();
</script>
<!-- 原理:http://suggestion.baidu.com/su?wd=wq&p=3&cb=window.bdsug.sug&t=1324271669786 -->
<!-- 那么百度返回:window.bdsug.sug({q:"wq",p:true,s:["wqvga","wqi","wqsg_umd","wqynyl","wqi下載","wqrc","wqnmlgb","wqvga分辨率","吳奇隆","吾棲之膚"]}); -->
</body>
</html>
?
StyleSheet.css
View Code #allSitesBoxHdl.classlist{
position: absolute;
background-color: #F5FBFF;
width: 256px;
border: 1px solid #C9E4F4;
top: 28px;
left: 0;
text-align: left;
font-size: 14px;
line-height: 30px;
padding: 1px;
}
#allSitesBoxHdl.classlist li
{
display: inline;
}
#allSitesBoxHdl.classlist li.lis a
{
text-decoration: none;
height: 30px;
width: 210px;
float: left;
padding-left: 8px;
color: #666;
}
#allSitesBoxHdl.classlist li.lis a:hover
{
color: #016493;
background-color: #edf6fb;
}
#allSitesBoxHdl.classlist li.lis a:active
{
color: #016493;
background-color: #edf6fb;
}
#allSitesBoxHdl.classlist li.lis input
{
cursor: pointer;
color: #FF6600;
border-right: 1px solid #ccc;
border-bottom: 1px solid #ccc;
height: 22px;
margin: 4px;
line-height: 22px;
float: right;
background: #fff;
}
.wena
{
color: #666;
font-size: 12px;
height: 30px;
line-height: 30px;
width: 250px;
float: left;
}
JScript2.js
//得到控件IDfunction getid(id) {
return (typeof id == 'string') ? document.getElementById(id) : id
};
function getOffsetTop(el, p) {
var _t = el.offsetTop;
while (el = el.offsetParent) {
if (el == p) break;
_t += el.offsetTop
}
return _t
};
function getOffsetLeft(el, p) {
var _l = el.offsetLeft;
while (el = el.offsetParent) {
if (el == p) break;
_l += el.offsetLeft
}
return _l
};
var currentInput = null;
//修改屬性顯示列表
function BoxShow(e) {
var input = e;
if (!input.id) {
input = e.target ? e.target : e.srcElement;
}
currentInput = input;
FillUrls();
var box = getid("allSitesBoxHdl");
if (box.style.display == 'block' && currentInput.id == input.id) {
return;
}
box.style.left = (getOffsetLeft(input)) + 'px';
box.style.top = (getOffsetTop(input) + (input.offsetHeight - 1)) + 'px';
box.style.width = (input.offsetWidth - 4) + 'px';
box.style.display = 'block';
}
//顯示列表
function BoxShowUrls(e) {
var input = e;
if (!input.id) {
input = e.target ? e.target : e.srcElement;
}
BoxShow(e);
}
//給Input設置值
function InputSetValue(val) {
var obj = currentInput;
obj.value = val;
if (obj.getAttribute('url') == 'true') {
var tags = document.getElementsByTagName('input');
for (var i = 0; i < tags.length; i++) {
if (tags[i].getAttribute('url') == 'true' && tags[i] != obj) {
tags[i].value = val;
}
}
}
BoxHide();
}
function BoxHide() {
if (getid("allSitesBoxHdl")) {
getid("allSitesBoxHdl").style.display = 'none';
}
}
//加載列表
function FillUrls() {
var strdomin = $.trim($("#Text1").val());
var qsData = { 'wd': strdomin, 'p': '3', 'cb': 'ShowDiv', 't': '1324113456725' };
$.ajax({
async: false,
url: "http://suggestion.baidu.com/su",
type: "GET",
dataType: 'jsonp',
jsonp: 'jsoncallback',
data: qsData,
timeout: 5000,
success: function (json) {
},
error: function (xhr) {
alert(xhr);
}
});
}
function ShowDiv(strurls) {
var urls = strurls["s"];
var html = "";
if (urls) {
var urllist = urls;
var forlength = 0;
var stringcookie;
for (var i = urllist.length - 1; i >= 0; i--) {
var textval = urllist[i];
if ($.trim(textval) != "" && $.trim(textval) != "undefined") {
html += "<li class=\"lis\"><a href=\"javascript:InputSetValue('" + textval + "');\">" + textval + "</a></li><br/>";
}
}
} else {
html = "<li style='font-size: 12px;' > 沒有記錄</li>";
}
if ($.trim(html) == "") {
html = "<li style='font-size: 12px;' > 沒有記錄</li>";
}
getid("allSitesBoxContent").innerHTML = html;
}
//關閉輸入法
function closeIME(e) {
var obj = e.target ? e.target : e.srcElement;
obj.style.imeMode = 'disabled';
}
function OnPaste(e) {
var obj = e.target ? e.target : e.srcElement;
setTimeout("MoveHttp('" + obj.id + "')", 100);
}
//修正URL
function MoveHttp(id) {
var val = getid(id).value;
val = val.replace("http://", "");
if (val[val.length - 1] == '/') {
val = val.substring(0, val.length - 1);
}
getid(id).value = val;
}
function OnKeyup(e) {
var obj = e.target ? e.target : e.srcElement;
setTimeout("addInput('" + obj.id + "')", 200);
}
//賦值
function addInput(id) {
var obj = getid(id);
//如果是一個沒有True的input不執行
if (obj.getAttribute('url') == 'true') {
if (obj.value.indexOf('。') > 0) {
obj.value = obj.value.replace('。', '.');
}
var tags = document.getElementsByTagName('input');
for (var i = 0; i < tags.length; i++) {
if (tags[i].getAttribute('url') == 'true' && tags[i] != obj) {
tags[i].value = obj.value;
}
}
}
FillUrls();
}
//注冊對象的事件
function Init() {
$("#allSitesBoxHdl")[0].style.display = "none";
$(":text").each(function () {
if ($(this)[0].getAttribute('url') == 'true') {//給所有的url=true屬性的Text加效果
$(this).bind("keyup", OnKeyup); //按鍵時
$(this).bind("mousedown", BoxShowUrls); //鼠標安下時
$(this).bind("mouseout", BoxHide); //鼠標離開時
$(this).bind("paste", OnPaste); //處理http;//
$(this)[0].setAttribute("autocomplete", "off");
}
});
}
?
?
文件下載:http://files.cnblogs.com/Music/BaiduSuggestionAPI.rar
謝謝瀏覽!
轉載于:https://www.cnblogs.com/Music/archive/2011/12/19/get-baidu-suggestion-api.html
總結
以上是生活随笔為你收集整理的调用百度“搜索建议(BaiduSuggestion)”的 API的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux iptables配置
- 下一篇: C语言实现与功能的程序,用C语言实现Pi