朋友有這么一個需求:他從網上查詢到自己的通話記錄,然后他想把所有北京的號碼調出來,可是查到的號碼不帶歸屬地,只能一個個的查詢,很麻煩,於是我寫了一段javascript代碼,可以批量查詢,代碼如下:
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>手機號碼歸屬地批量查詢</title>
</head>
<body>
<script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
<script language="javascript">
var timeout = 5;//每條記錄查詢延時時間,單位:秒
var strContent = "";
var strArray = "";
function getInfo()
{
strContent = document.getElementById("strContent").value;
strArray = strContent.split(navigator.appName=="Netscape"?"\n":"\r\n");
$("#contentDiv").html("");
getCity(0);
}
function getCity(i)
{
var num = strArray[i].replace(new RegExp(" ","g"),"").replace(new RegExp("\t","g"),"");
$.ajax({
type: "POST",
dataType:"jsonp",
url: "http://api.showji.com/Locating/20120413.aspx",
data: "m=" + num + "&output=json",
jsonpCallback:"querycallback",
success: function(msg){
$("#contentDiv").html($("#contentDiv").html() + "號碼:" + num + " 城市:" + msg.City + "<br />");
i++;
if(i < strArray.length) {
setTimeout("getCity(" + i + ")", timeout * 1000);
}
}
});
}
</script>
<form id="form1" name="form1" method="post" action="">
<p>請輸入手機號碼(多條記錄用回車換行):<br />
<textarea name="strContent" id="strContent" cols="80" rows="12"></textarea>
<input type="button" name="Button" value="查詢號碼所在城市" onclick="getInfo()" />
</p>
<p id="contentDiv"> </p>
</form>
</body>
</html>
代碼打包下載:http://files.cnblogs.com/modou/20120420.rar
注意內容:
1、代碼用到了jquery,需要把jquery的對應代碼添加進來
2、我使用的這個網址:http://api.showji.com/Locating/20120413.aspx 抓取的手機歸屬地,如果該網址發生變化,可以再重新到http://www.showji.com/ 獲取相關網址,這個網址可以在該網站查詢的時候,開啟Fiddler2工具監控到。
3、可能是http://www.showji.com/ 做了限制,在一定時間內查詢的次數過多就查詢不到對應的結果了,我把查詢時間的間隔設為5秒鍾,如果號碼比較多,等待的時間會比較長一些
