PinPHP購物分享系統(tǒng)2.2后臺批量采集修改實(shí)現(xiàn)方法
來源:程序員人生 發(fā)布時間:2014-06-18 23:26:26 閱讀次數(shù):2864次
因前段時間較忙,所以一直將這開發(fā)擱置了。今天看了一下新版的PinPHP,又心血來潮于是寫了一下這個批量采集的實(shí)現(xiàn),沒想到寫了差不多一兩小時就實(shí)現(xiàn)了,雖然寫得比較簡單,也算是可以幫助一鍵采集一個分類。同時非常感謝PinPHP團(tuán)隊(duì)開發(fā)出
如此好使的開源程序,哈,閑話先不多說,上代碼。
附源文件:下載源代碼請猛擊這里>>
主要是對一個模板文件作了修改。/PinPHP_V2.21/admin/Tpl/default/items_collect/collect.html
增加了“批量采集當(dāng)前分類”按鈕:
<div style="display:inline-block; margin-left:30px; width:650px">
<input type="button" value="批量采集當(dāng)前分類" id="btnCollectCurrent" style="padding:5px 10px; margin-right:30px;" />
<img src="http://meego123.net/__ROOT__/statics/images/masonry_loading.gif" id="collecting" style="display:none" />
<span id="info"></span>
</div>
JS實(shí)現(xiàn):
(function($){
//當(dāng)前
var argArray,str,strSplit,strUrl,collectTimer,nLen,count=0,errCount=0;
$("#btnCollectCurrent").bind("click",function(){
argArray = new Array();
$("#items_cate_list tr").each(function(index, element) {
$(this).children("td").each(function(index, element) {
var aTag = $(this).children("a");
if(aTag.length>0){
var str=$(aTag).attr("href").replace("javascript:collect(","").replace(")","").replace(";","").replace("'","").replace("'","");
var strSplit = str.split(",");
var strUrl = '?page=1&a=taobao_collect_jump&m=items_collect&cate_id='+strSplit[0]+'&keywords='+$.trim(strSplit[1]);
argArray.push(strUrl);
}
});
});
$("#collecting").show();
$("#info").text("采集中...");
nLen = argArray.length;
collectTimer = setInterval(function(){
if(count>=nLen) {
clearInterval(collectTimer);
$("#collecting").hide();
$("#info").text("采集完成");
return;
}
$.ajax({
url:argArray[count],
type:"GET",
dataType:"Html",
async:false,
success:function(){
$("#info").text("采集中...共"+nLen+"個 當(dāng)前正在執(zhí)行第"+count+"個采集 失敗"+errCount+"個");
count++;
},
error:function(){
errCount++;
count++;
}
});
},1000);
});
})(jQuery);
實(shí)現(xiàn)效果:

目前只實(shí)現(xiàn)了采集當(dāng)前選中分頁的子類批量采集。
從代碼可以看出,也是拼接采集的URL,然后通過Ajax定時請求,這次用了同步采集,測試中發(fā)現(xiàn)采用異步時
其中有些分類會采集失敗,可能因?yàn)檎埱蟊恢械?。采集間隔設(shè)置為1秒,為保證請求完成,并在過多類時,防止阻塞
導(dǎo)致瀏覽器假死。若要采集多頁可修改page參數(shù),暫設(shè)定為1頁
var strUrl = '?page=1&a=taobao_collect_jump&m=items_collect&cate_id='+strSplit[0]+'&keywords='+$.trim(strSplit[1]);
附源文件:下載源代碼請猛擊這里>>
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈