10.Save Length
关联出来的内容所需要保存的长度。在Save Offset的例子中我们写到如何获得successfully installed XAMPP on this system!这个字符串,如果我们还希望获得这个字符串中的successfully installed XAMPP,那么可以再添加Save Length为22,代码变为:
web_reg_save_param_ex( "ParamName=temp", "LB=Congratulations:<br>", "RB=</b><p>", "Ordinal=1", "SaveOffset=9", "SaveLen=22", SEARCH_FILTERS, "ContentType=text/html", LAST); |
通过Save Length和Save Offset的设置,我们就可以方便地抓取服务器返回的定长数据的任意一个部分了。
关联可以调整偏移量和长度,那么参数能做到吗?当然可以,如果需要对一个参数值进行偏移和长度设置,则需要使用lr_save_var函数,例如,下面的代码:
lr_save_string("I come from shanghai","city"); lr_save_var(lr_eval_string("{city}"),6,0,"result"); //从city这个参数中取6位长度的内容保存到result参数中 lr_save_var(lr_eval_string("{city}")+7,4,0,"result"); //从city这个参数的第7位开始取4个长度的内容保存到result参数中 |
可以看到运行的结果是:
Action.c(3): Notify: Saving Parameter "city = I come from shanghai" Action.c(4): Notify: Saving Parameter "result = I come" Action.c(5): Notify: Saving Parameter "result = from" |
11.Warm if text was not found (Default is Error)
如果关联的对象不存在,又该如何进行处理呢?默认值为Error,默认情况下如果没有关联到任何内容则提示错误。
Action.c(27): Error -26377: No match found for the requested parameter "temp". Check whether the requested boundaries exist in the response data. Also, if the data you want to save exceeds 256 bytes, use web_set_max_html_param_len to increase the parameter size [MsgId: MERR-26377]
而选择该项,则只会简单提示没有抓到内容,不会产生错误。
Action.c(28): Warning -26377: No match found for the requested parameter "temp". Check whether the requested boundaries exist in the response data. Also, if the data you want to save exceeds 256 bytes, use web_set_max_html_param_len to increase the parameter size [MsgId: MWAR-26377] |
在很多时候关联不到内容也是正常的。例如,需要关联一个板块中的帖子编号,就存在没有帖子的情况。为了确保关联的正确性,在某些情况下可以选中该选项,出现的提示方式为WARNING,并且为了确保没有帖子时不会对后面的操作产生影响,需要编写一个if语句进行判断,如下所示:
Action() { web_reg_save_param_ex( "ParamName=tid", "LB=<a href=\"read.php?tid=", "RB=&page=", SEARCH_FILTERS, LAST); web_url("topiclist","URL=http://localhost:8000/phpwind85/thread.php?fid=2",LAST); web_url("read","URL=http://localhost:8000/phpwind85/read.php?tid={tid}",LAST); return 0; } |
上面这种写法是平常最通常的写法,接着我们添加分支判断,代码如下:
web_reg_save_param_ex( "ParamName=tid", "LB=<a href=\"read.php?tid=", "RB=&page=", "NotFound=warning", SEARCH_FILTERS, LAST); web_url("topiclist","URL=http://localhost:8000/phpwind85/thread.php?fid=64",LAST); if (strcmp(lr_eval_string("{tid}"),"")==0) { lr_output_message("there is no any topicid in the forum"); } else { web_url("read","URL=http://localhost:8000/phpwind85/read.php?tid={tid}",LAST); } |
当我们添加了分支判断后,当板块没有帖子时,不会出现错误了,也不会去错误地访问后面的阅读帖子操作,转向执行信息输出。
如果关联选项Ord为All时,这里if语句检查的对象应该修改为tid_ count参数,代码变为:
web_reg_save_param_ex( "ParamName=tid", "LB=<a href=\"read.php?tid=", "RB=&page=", "NotFound=warning", "Ordinal=ALL", SEARCH_FILTERS, LAST); web_url("topiclist","URL=http://localhost:8000/phpwind85/thread.php?fid=2",LAST); if (atoi(lr_eval_string("{tid_count}"))==0) { lr_output_message("there is no any topicid in the forum"); } else { lr_save_string(lr_paramarr_random(“tid”),”rtid”); web_url("read","URL=http://localhost:8000/phpwind85/read.php?tid={rtid}",LAST); } |
这个代码可以实现在板块中随机选帖子,如果没帖子则不做操作。