频道脚本是频道配置中的脚本, 若是配置了频道脚本,该频道的全部采集流程将被配置的脚本所接管。php
一.可用全局对象(只读)html
EXTRACT:当前采集引擎[ 对象类型: extractor ]jquery
DATADB:当前链接的数据库[ 对象类型: dataBase ]ajax
RESULT:当前结果集对象[ 对象类型: result ]数据库
二.this指针json
当前频道节点[channel ]对象服务器
三.脚本返回值this
无url
示例一:用脚本建立一个采集源列表指针
1.如下脚本将生成 http://xjrb.xjrb.com/xjrb/20141201/index.htm~ http://xjrb.xjrb.com/xjrb/20141231/index.htm 共31条连接:
url u;
for(i=1;i <=31;i++)
{
u.entryid = this.id;//频道
idu.tmplid =1;//模板
Idu.urlname ="http://xjrb.xjrb.com/xjrb/201412"+ i.Dim(2) +"/index.htm";//连接地址
u.title ="test";
RESULT.AddLink(u);
//添加到最后的结果中
}
2.如下脚本将生成从当前日期递推前十天的连接:
url u;
time t1;
for(i=0;i<10;i++)
{
u.title ="test";//连接标题
u.entryid = this.id;//频道
idu.tmplid =1;//模板
Idpre = t1.Preday(i);//向前计算日期
u.urlname ="http://www.cdrb.com.cn/html/"+ pre.year +"-"+ pre.month +"/"+ pre.day +"/content_2155799.htm";//连接地址
RESULT.AddLink(u);//添加到最后的结果中
}
3.如下脚本用关键词拼接连接:
url u;var keys=["前嗅","爬虫"];for(i=0;i
示例二:用脚本采集数据
1.如下脚本查找表格并抽取表格数据:
gdoc = EXTRACT.OpenDoc(this,"http://gk.sjtu.edu.cn/index.php/list/fellow/2015-10-30-15-02-59/241-2015-11-18-02-21-01",0);
if(gdoc){dm = gdoc.GetDom();
record rec;
if(dm)
{
tab = dm.FindName("table"); if(tab) { tr = dm.FindName("tr", tab); while(tr) { name = dm.FindName("td", tr); if(name) {//找到数据 posd =0; corp=0;fund=0; rec.name = dm.GetTextAll(name);//名字 posd = name.next; if(posd) {corp = posd.next; rec.position = dm.GetTextAll(posd); } if(corp) { fund = corp.next; rec.company = dm.GetTextAll(corp); } if(fund) { rec.fund = dm.GetTextAll(fund); }
RESULT.AddRec(rec,3);
}
tr = tr.next;
}
}
}
EXTRACT.CloseDoc(gdoc);
}
2.如下脚本从服务器请求json数据并存入到记录中:
gdoc = EXTRACT.OpenDoc(this,"http://www.w3school.com.cn//example/jquery/demo_ajax_json.js",0);
if(gdoc)
{
jScript js;
record rec;
data = js.RunJson(gdoc.GetDom().GetSource());
rec.name = data.firstName;
rec.family=data.lastName;
rec.age = data.age;
schea = EXTRACT.GetSchema("schemaName"); //获取表单
IDif(schea) sId = schea.id;
elsesId = 1;
RESULT.AddRec(rec,sId);
EXTRACT.CloseDoc(gdoc);
}