下面是浏览帖子的一个Action录制后经过简单修改的初始脚本。
- viewtopic()
- {
- //进入某个版块
- web_url("forum.php_3",
- "URL=http://192.168.159.144/bbs/forum.php?mod=forumdisplay&fid=38",
- "Resource=0",
- "RecContentType=text/html",
- "Referer=http://192.168.159.144/bbs/forum.php",
- "Snapshot=t122.inf",
- "Mode=HTML",
- LAST);
- //点击某个帖子
- web_url("forum.php_4",
- "URL=http://192.168.159.144/bbs/forum.php?mod=viewthread&tid=997&extra=page%3D1",
- "Resource=0",
- "RecContentType=text/html",
- "Referer=http://192.168.159.144/bbs/forum.php?mod=forumdisplay&fid=2",
- "Snapshot=t125.inf",
- "Mode=HTML",
- EXTRARES,
- "Url=static/image/common/share_btn.png", "Referer=http://192.168.159.144/bbs/forum.php?mod=viewthread&tid=997&extra=page%3D1", ENDITEM,
- "Url=static/image/common/midavt_shadow.gif", "Referer=http://192.168.159.144/bbs/forum.php?mod=viewthread&tid=997&extra=page%3D1", ENDITEM,
- "Url=static/image/common/fastreply.gif", "Referer=http://192.168.159.144/bbs/forum.php?mod=viewthread&tid=997&extra=page%3D1", ENDITEM,
- "Url=static/image/common/edit.gif", "Referer=http://192.168.159.144/bbs/forum.php?mod=viewthread&tid=997&extra=page%3D1", ENDITEM,
- LAST);
- }
在上面脚本中,点击的板块的fid与帖子的tid都是死的。这两个地方需要参数化和关联。
1.参数化板块的fid
如下图
然后将进入板块的fid的值替换为参数。如下
- web_url("forum.php_3",
- "URL=http://192.168.159.144/bbs/forum.php?mod=forumdisplay&fid={fid}",
- "Resource=0",
- "RecContentType=text/html",
- "Referer=http://192.168.159.144/bbs/forum.php",
- "Snapshot=t122.inf",
- "Mode=HTML",
- LAST);
经过上面的对板块的fid值进行参数化后,每次迭代就会进入不同的板块,但是后面点击的帖子的tid是死了,不能做到进入哪个板块就点击哪个帖子的效果。所以要对tid进行关联。
我们首先查看下板块页面的html源代码,确定关联的左右边界。我将关键部分做了截图
图中箭头所指的部分就是需要关联的内容。
2.1 获取单个参数
首先在点击帖子之前执行 web_reg_save_param()函数,将找到的帖子的tid放入topictid参数中
- <strong> web_reg_save_param("topictid",
- "LB=<a href=\"forum.php?mod=viewthread&tid=",
- "RB=&extra=page%3D1\" onclick=\"atarget(this)\" class=\"xst\" >",
- "Search=NoResource",
- "Notfound=warning",
- "ORD=1",
- LAST);
然后在点击帖子的时候使用topictid参数对帖子tid进行参数化。在此之前还需要判断此时板块里有没有帖子,没有帖子退出程序,有帖子的话执行web_url函数。
整体脚本如下:
- viewtopic()
- {
- //查找出第一个帖子的tid,将此作为参数
- web_reg_save_param("topictid",
- "LB=<a href=\"forum.php?mod=viewthread&tid=",
- "RB=&extra=page%3D1\" onclick=\"atarget(this)\" class=\"xst\" >",
- "Search=NoResource",
- "Notfound=warning",
- "ORD=1",
- LAST);
- //进入某个版块
- web_url("forum.php_3",
- "URL=http://192.168.159.144/bbs/forum.php?mod=forumdisplay&fid={fid}",
- "Resource=0",
- "RecContentType=text/html",
- "Referer=http://192.168.159.144/bbs/forum.php",
- "Snapshot=t122.inf",
- "Mode=HTML",
- LAST);
- //如果板块中没有帖子,直接退出脚本,如果有帖子,进行点击
- if ( strcmp(lr_eval_string("{topictid}"),"") == 0 ) {
- return 0;
- }else
- {
- web_url("forum.php_4",
- "URL=http://{IP}/bbs/forum.php?mod=viewthread&tid={topictid}&extra=page%3D1",
- "Resource=0",
- "RecContentType=text/html",
- "Referer=http://{IP}/bbs/forum.php?mod=forumdisplay&fid=2",
- "Snapshot=t125.inf",
- "Mode=HTML",
- EXTRARES,
- "Url=static/image/common/share_btn.png", "Referer=http://{IP}/bbs/forum.php?mod=viewthread&tid=997&extra=page%3D1", ENDITEM,
- "Url=static/image/common/midavt_shadow.gif", "Referer=http://{IP}/bbs/forum.php?mod=viewthread&tid=997&extra=page%3D1", ENDITEM,
- "Url=static/image/common/fastreply.gif", "Referer=http://{IP}/bbs/forum.php?mod=viewthread&tid=997&extra=page%3D1", ENDITEM,
- "Url=static/image/common/edit.gif", "Referer=http://{IP}/bbs/forum.php?mod=viewthread&tid=997&extra=page%3D1", ENDITEM,
- LAST);
- }
- return 0;
- }
2.2 获取参数数组
前面的方法有一个缺点,它只能点击某个板块的第一个帖子。这显然不是我们想要的结果。我们想要结果是能够随机点击某个板块的一个帖子。这时需要用到参数数组。
也就是执行 web_reg_save_param()函数的时候将某个板块首页的全部帖子的tid保存到一个参数数组中。脚本如下:
- web_reg_save_param("topictid",
- "LB=<a href=\"forum.php?mod=viewthread&tid=",
- "RB=&extra=page%3D1\" onclick=\"atarget(this)\" class=\"xst\" >",
- "Search=NoResource",
- "Notfound=warning",
- "ORD=ALL",
- LAST);
对照可以看出,此处把ORD=1,修改为了ORD=ALL。
修改后整体脚本如下:
- viewtopic()
- {
- //用于记录参数数组的长度
- int len;
- //用于记录参数数组中随机取出的一个参数的值
- char * topic;
- //查找出所有帖子的的tid,并将此放入参数数组中
- web_reg_save_param("topictid",
- "LB=<a href=\"forum.php?mod=viewthread&tid=",
- "RB=&extra=page%3D1\" onclick=\"atarget(this)\" class=\"xst\" >",
- "Search=NoResource",
- "Notfound=warning",
- "ORD=ALL",
- LAST);
- //进入某个版块
- web_url("forum.php_3",
- "URL=http://{IP}/bbs/forum.php?mod=forumdisplay&fid={fid}",
- "Resource=0",
- "RecContentType=text/html",
- "Referer=http://{IP}/bbs/forum.php",
- "Snapshot=t122.inf",
- "Mode=HTML",
- LAST);
- //将数组长度的值赋值给len变量
- len = atoi(lr_eval_string("{topictid_count}"));
- //将参数数组中随机取出的一个值赋给topic变量
- topic = lr_paramarr_random("topictid");
- //将参数数组中随机取出的值保持为randomtopic参数
- lr_save_string( topic,"randomtopic");
- if( len == 0)
- {
- return 0;
- }else
- {
- web_url("forum.php_4",
- "URL=http://{IP}/bbs/forum.php?mod=viewthread&tid={randomtopic}&extra=page%3D1",
- "Resource=0",
- "RecContentType=text/html",
- "Referer=http://{IP}/bbs/forum.php?mod=forumdisplay&fid=2",
- "Snapshot=t125.inf",
- "Mode=HTML",
- EXTRARES,
- "Url=static/image/common/share_btn.png", "Referer=http://{IP}/bbs/forum.php?mod=viewthread&tid=997&extra=page%3D1", ENDITEM,
- "Url=static/image/common/midavt_shadow.gif", "Referer=http://{IP}/bbs/forum.php?mod=viewthread&tid=997&extra=page%3D1", ENDITEM,
- "Url=static/image/common/fastreply.gif", "Referer=http://{IP}/bbs/forum.php?mod=viewthread&tid=997&extra=page%3D1", ENDITEM,
- "Url=static/image/common/edit.gif", "Referer=http://{IP}/bbs/forum.php?mod=viewthread&tid=997&extra=page%3D1", ENDITEM,
- LAST);
- }
- return 0;
- }
在此说明下参数数组。我们可以通过下面的方法生成一个参数数组。
- lr_save_string("www.baidu.com","site_1");
- lr_save_string("www.sina.com","site_2");
- lr_save_string("www.163.com","site_3");
- lr_save_string("www.qq.com","site_4");
- lr_save_string("4","site_count");
此时参数数组名为site,各个参数的名为site_1,site_2,site_3,site_4。在一个参数数组中,有一个参数用来记录参数数组的长度。即参数名_count。