LR mobile HTTP/HTML协议实战

 下面我们来看一下手动关联后的脚本情况:

最好不要用自动关联,虽然LR11.5已经准确了不少,但还是会出现截取不准确的情况。

C代码 复制代码 收藏代码
  1. <SPAN>Action()
  2. {
  3. char DistrictName[15]={NULL},
  4. CityCode[20]={NULL},
  5. ProvinceCode[20]={NULL},
  6. DistrictCode[20]={NULL};
  7. int DistrictSequence=0;
  8. web_add_cookie("JSESSIONID=6B75C5017659AA3BFB2C89746A16D20F; DOMAIN=m.weathercn.com");
  9. </SPAN>
Action()
{
char  DistrictName[15]={NULL},
        CityCode[20]={NULL},
        ProvinceCode[20]={NULL},
        DistrictCode[20]={NULL};
int     DistrictSequence=0;
web_add_cookie("JSESSIONID=6B75C5017659AA3BFB2C89746A16D20F; DOMAIN=m.weathercn.com");

/* type url,the default city code is shanghai, you can parameterize it */ /*这里默认定位是上海,可通过后面的关联将省代码获取到,然后进行参数化避免每次打开都是上海'*/

C代码 复制代码 收藏代码
  1.  
 
C代码 复制代码 收藏代码
  1. <SPAN style="FONT-FAMILY: 'Microsoft YaHei',微软雅黑,SimHei,tahoma,arial,helvetica,sans-serif">web_url("m.weathercn.com",
  2. "URL=http://m.weathercn.com/?cid=01012601",
  3. "Resource=0",
  4. "RecContentType=text/html",
  5. "Referer=",
  6. "Snapshot=t1.inf",
  7. "Mode=HTTP",
  8. LAST);
  9. </SPAN>
web_url("m.weathercn.com",  		"URL=http://m.weathercn.com/?cid=01012601",  		"Resource=0",  		"RecContentType=text/html",  		"Referer=",  		"Snapshot=t1.inf",  		"Mode=HTTP",  		LAST); 
C代码 复制代码 收藏代码
  1. <SPAN> /*这里有一点疑问,之前看自动关联LR生成的关联中写的为RegExp=pid=(.*?)\ \\" class,一直没有弄明白,这里为什么会多出一个\\而仍然可以关联成功*/
  2. /*关联以获取到所有的省代码,LR在C语言中并不推荐使用web_reg_save_param_ex函数,这里用其实也是可以通过的*/</SPAN>
 /*这里有一点疑问,之前看自动关联LR生成的关联中写的为RegExp=pid=(.*?)\ \\" class,一直没有弄明白,这里为什么会多出一个\\而仍然可以关联成功*/ 
/*关联以获取到所有的省代码,LR在C语言中并不推荐使用web_reg_save_param_ex函数,这里用其实也是可以通过的*/
C代码 复制代码 收藏代码
  1. <SPAN>
  2. /*select province*/
  3. lr_start_transaction("Defaultpage");
  4. web_url("Province",
  5. "URL=http://m.weathercn.com/province.jsp",
  6. "Resource=0",
  7. "RecContentType=text/html",
  8. "Referer=http://m.weathercn.com/?cid=01012601",
  9. "Snapshot=t2.inf",
  10. "Mode=HTTP",
  11. LAST);
  12. lr_end_transaction("Defaultpage",LR_AUTO);
  13.  
  14.  
  15. lr_paramarr_len("ProvincesCode"); /*'输出数组长度'*/
  16. /*save random procince code as a parameter*/
  17. sprintf(ProvinceCode,"%s",lr_paramarr_random("ProvincesCode"));
  18. /*根据数组内容来随机获取并保存为变量*/
  19. lr_save_string(lr_eval_string(ProvinceCode),"ProvinceCode");
  20. /*将变量保存为参数,这一步是必需的,否则将无法参数化*/
  21. lr_output_message("%s",lr_eval_string("{ProvinceCode}"));
  22.  
  23.  
  24.  
  25. /*obtain city code,web_reg_save_param can function well here, but not suggested */
  26. web_reg_save_param(
  27. "CitiesCode",
  28. "LB=did=",
  29. "RB=&pid=",
  30. "ORD=all",
  31. LAST);
  32.  
  33. /*select city */
  34. lr_start_transaction("SelectCity");
  35. web_url("City",
  36. "URL=http://m.weathercn.com/dis.do?pid={ProvinceCode}", /* the default value is 0101114*/
  37. "Resource=0",
  38. "RecContentType=text/html",
  39. "Referer=http://m.weathercn.com/province.jsp",
  40. "Snapshot=t3.inf",
  41. "Mode=HTTP",
  42. LAST);
  43. lr_end_transaction("SelectCity",LR_AUTO);
  44.  
  45.  
  46. /* save city code as a parameter*/
  47. sprintf(CityCode,"%s",lr_paramarr_random("CitiesCode"));
  48. lr_save_string(lr_eval_string(CityCode),"CityCode");
  49. lr_output_message("%s",lr_eval_string("{CityCode}"));
  50.  
  51. /*obtain district code*/
  52. web_reg_save_param(
  53. "DistrictsCode",
  54. "LB=cid=",
  55. "RB=&pid",
  56. "ORD=all",
  57. LAST);
  58.  
  59.  
  60. /*obtain district name for text check.*/
  61. web_reg_save_param_regexp(
  62. "paramName=DistrictsName",
  63. "RegExp=class=\"c\">(.{4,8})</a>\r\n", /*match Chinese character use ([\u4e00-\u9fa5]{4,6}).. because chinese character display inproper here */
  64. "Ordinal=all",
  65. LAST);
  66.  
  67.  
  68. .
  69. .
  70. .
  71. .
  72. .
  73. .
  74. .
  75. '中间太长了我就不贴了,操作都是类似的。下面通过是将具体的省、市、区保存为参数来进行参数化'
  76.  
  77.  
  78. /* generate random number to acquire a district name corresponding with district code*/
  79. srand(time(NULL));
  80. DistrictSequence=(rand()%(lr_paramarr_len("DistrictsCode")))+1;
  81. /*save district name and district code as a parameter*/
  82. lr_output_message("%d",DistrictSequence);
  83. sprintf(DistrictName,"{DistrictsName_%d}",DistrictSequence);
  84. lr_save_string(lr_eval_string(DistrictName),"DistrictName");
  85. sprintf(DistrictCode,"{DistrictsCode_%d}",DistrictSequence);
  86. lr_save_string(lr_eval_string(DistrictCode),"DistrictCode");
  87. lr_output_message("%s",lr_eval_string("{DistrictName}"));
  88. lr_output_message("%s",lr_eval_string("{DistrictCode}"));
  89.  
  90.  
  91. /*text check*/
  92. /*设置文本检查点,下好从上面保存为参数的DistrictName中获取*/
  93. web_reg_find("Search=Body",
  94. "Text={DistrictName}",
  95. LAST);
  96. /*weather report*/
  97. lr_start_transaction("ReportPage");
  98. web_url("WeatherReport",
  99. "URL=http://m.weathercn.com/index.do?cid={DistrictCode}&pid={ProvinceCode}", /* default cid=0101141012 pid=0101114*/
  100. "Resource=0",
  101. "RecContentType=text/html",
  102. "Referer=http://m.weathercn.com/cout.do?did={CityCode}&pid={ProvinceCode}",
  103. "Snapshot=t5.inf",
  104. "Mode=HTTP",
  105. LAST);
  106. lr_end_transaction("ReportPage",LR_AUTO);
  107.  
  108.  
  109. /*以下内容没有进行参数化,以下内容主要是一些像显示天气状态的图片内容,之前看到过一篇文件说关于web_concurrent_start只是提交的数据有区别,所以没有必要进行参数化' */
  110. /* you can parameterize the following value */
  111. web_concurrent_start(NULL);
  112.  
  113. web_url("Day02.png",
  114. "URL=http://m.weathercn.com/fh30/Day02.png",
  115. "Resource=1",
  116. "RecContentType=image/png",
  117. "Referer=http://m.weathercn.com/index.do?cid=0101141012&pid=010114",
  118. LAST);
  119.  
  120. web_url("Day00.png",
  121. "URL=http://m.weathercn.com/fh100/Day00.png",
  122. "Resource=1",
  123. "RecContentType=image/png",
  124. "Referer=http://m.weathercn.com/index.do?cid=0101141012&pid=010114",
  125. LAST);
  126.  
  127. web_url("Night02.png",
  128. "URL=http://m.weathercn.com/fh30/Night02.png",
  129. "Resource=1",
  130. "RecContentType=image/png",
  131. "Referer=http://m.weathercn.com/index.do?cid=0101141012&pid=010114",
  132. LAST);
  133.  
  134. web_concurrent_end(NULL);
  135.  
  136. return 0;
  137. }
  138. 下面我们就来简单的看一个服务器返回的内容来如何进行参数化。</SPAN>
   	
/*select province*/
lr_start_transaction("Defaultpage");     
web_url("Province",                                                 
		"URL=http://m.weathercn.com/province.jsp", 
		"Resource=0", 
		"RecContentType=text/html", 
		"Referer=http://m.weathercn.com/?cid=01012601", 
		"Snapshot=t2.inf", 
		"Mode=HTTP", 
		LAST);
  lr_end_transaction("Defaultpage",LR_AUTO);
   
   
lr_paramarr_len("ProvincesCode"); /*'输出数组长度'*/
/*save random procince code as a parameter*/
 sprintf(ProvinceCode,"%s",lr_paramarr_random("ProvincesCode"));
/*根据数组内容来随机获取并保存为变量*/
 lr_save_string(lr_eval_string(ProvinceCode),"ProvinceCode");
/*将变量保存为参数,这一步是必需的,否则将无法参数化*/
lr_output_message("%s",lr_eval_string("{ProvinceCode}"));
	
	
	
/*obtain city code,web_reg_save_param can function well here, but not suggested */
	web_reg_save_param(
	"CitiesCode",
	"LB=did=",
	"RB=&pid=",
	"ORD=all",
	LAST);
	
/*select city */
lr_start_transaction("SelectCity");
web_url("City", 
		"URL=http://m.weathercn.com/dis.do?pid={ProvinceCode}",   /* the default value is 0101114*/
		"Resource=0", 
		"RecContentType=text/html", 
		"Referer=http://m.weathercn.com/province.jsp", 
		"Snapshot=t3.inf", 
		"Mode=HTTP", 
		LAST);
lr_end_transaction("SelectCity",LR_AUTO);
	
	
/* save city code as a parameter*/
sprintf(CityCode,"%s",lr_paramarr_random("CitiesCode"));
lr_save_string(lr_eval_string(CityCode),"CityCode");
lr_output_message("%s",lr_eval_string("{CityCode}"));
	
/*obtain district code*/
web_reg_save_param(
	"DistrictsCode",
	"LB=cid=",
	"RB=&pid",
	"ORD=all",
	LAST);
	
	
/*obtain district name for text check.*/
web_reg_save_param_regexp(
	"paramName=DistrictsName",
	"RegExp=class=\"c\">(.{4,8})</a>\r\n",   /*match Chinese character use ([\u4e00-\u9fa5]{4,6}).. because chinese character display inproper here */
	"Ordinal=all",
	LAST);
	

.
.
.
.
.
.
.
'中间太长了我就不贴了,操作都是类似的。下面通过是将具体的省、市、区保存为参数来进行参数化'	

	
/* generate random number to acquire a district name corresponding with district code*/
srand(time(NULL));
DistrictSequence=(rand()%(lr_paramarr_len("DistrictsCode")))+1;
/*save district name and district code as a parameter*/
lr_output_message("%d",DistrictSequence);
sprintf(DistrictName,"{DistrictsName_%d}",DistrictSequence);
lr_save_string(lr_eval_string(DistrictName),"DistrictName");
sprintf(DistrictCode,"{DistrictsCode_%d}",DistrictSequence);
lr_save_string(lr_eval_string(DistrictCode),"DistrictCode");
lr_output_message("%s",lr_eval_string("{DistrictName}"));
lr_output_message("%s",lr_eval_string("{DistrictCode}"));
	
	
/*text check*/
/*设置文本检查点,下好从上面保存为参数的DistrictName中获取*/
web_reg_find("Search=Body",
	        "Text={DistrictName}",
	         LAST);
/*weather report*/
lr_start_transaction("ReportPage");
web_url("WeatherReport", 
		"URL=http://m.weathercn.com/index.do?cid={DistrictCode}&pid={ProvinceCode}", /* default cid=0101141012  pid=0101114*/
		"Resource=0", 
		"RecContentType=text/html", 
		"Referer=http://m.weathercn.com/cout.do?did={CityCode}&pid={ProvinceCode}", 
		"Snapshot=t5.inf", 
		"Mode=HTTP", 
		LAST);
lr_end_transaction("ReportPage",LR_AUTO);
    
    
/*以下内容没有进行参数化,以下内容主要是一些像显示天气状态的图片内容,之前看到过一篇文件说关于web_concurrent_start只是提交的数据有区别,所以没有必要进行参数化'  */ 
/* you can parameterize the following value */
web_concurrent_start(NULL);

web_url("Day02.png", 
		"URL=http://m.weathercn.com/fh30/Day02.png", 
		"Resource=1", 
		"RecContentType=image/png", 
		"Referer=http://m.weathercn.com/index.do?cid=0101141012&pid=010114", 
		LAST);

web_url("Day00.png", 
		"URL=http://m.weathercn.com/fh100/Day00.png", 
		"Resource=1", 
		"RecContentType=image/png", 
		"Referer=http://m.weathercn.com/index.do?cid=0101141012&pid=010114", 
		LAST);

web_url("Night02.png", 
		"URL=http://m.weathercn.com/fh30/Night02.png", 
		"Resource=1", 
		"RecContentType=image/png", 
		"Referer=http://m.weathercn.com/index.do?cid=0101141012&pid=010114", 
		LAST);

web_concurrent_end(NULL);

return 0;
}
下面我们就来简单的看一个服务器返回的内容来如何进行参数化。
Html代码 复制代码 收藏代码
  1. <SPAN style="FONT-FAMILY: 'Microsoft YaHei',微软雅黑,SimHei,tahoma,arial,helvetica,sans-serif"><ul id="more">
  2. <li><a href="dis.do?pid=010101" class="c">??</a>|<a href="dis.do?pid=010126" class="c">??</a>|<a href="dis.do?pid=010129" class="c">??</a></li>
  3. <li><a href="dis.do?pid=010104" class="c">??</a>|<a href="dis.do?pid=010116" class="c">??</a>|<a href="dis.do?pid=010113" class="c">???</a></li>
  4. <li><a href="dis.do?pid=010119" class="c">??</a>|<a href="dis.do?pid=010120" class="c">???</a>|<a href="dis.do?pid=010132" class="c">??</a></li>
  5. <li><a href="dis.do?pid=010121" class="c">??</a>|<a href="dis.do?pid=010122" class="c">??</a>|<a href="dis.do?pid=010106" class="c">??</a></li>
  6. <li><a href="dis.do?pid=010125" class="c">??</a>|<a href="dis.do?pid=010130" class="c">??</a>|<a href="dis.do?pid=010127" class="c">??</a></li>
  7. <li><a href="dis.do?pid=010109" class="c">??</a>|<a href="dis.do?pid=010133" class="c">??</a>|<a href="dis.do?pid=010124" class="c">??</a></li>
  8. <li><a href="dis.do?pid=010111" class="c">??</a>|<a href="dis.do?pid=010123" class="c">??</a>|<a href="dis.do?pid=010112" class="c">??</a></li>
  9. <li><a href="dis.do?pid=010102" class="c">??</a>|<a href="dis.do?pid=010117" class="c">??</a>|<a href="dis.do?pid=010114" class="c">??</a></li>
  10. <li><a href="dis.do?pid=010115" class="c">??</a>|<a href="dis.do?pid=010118" class="c">??</a>|<a href="dis.do?pid=010134" class="c">??</a></li>
  11. <li><a href="dis.do?pid=010105" class="c">??</a>|<a href="dis.do?pid=010128" class="c">??</a>|<a href="dis.do?pid=010108" class="c">??</a></li>
  12. <li><a href="dis.do?pid=010110" class="c">??</a>|<a href="dis.do?pid=010107" class="c">??</a>|<a href="dis.do?pid=010131" class="c">??</a></li>
  13. <li><a href="dis.do?pid=010103" class="c">??</a></li></SPAN>
<ul id="more">     <li><a href="dis.do?pid=010101" class="c">??</a>|<a href="dis.do?pid=010126" class="c">??</a>|<a href="dis.do?pid=010129" class="c">??</a></li>     <li><a href="dis.do?pid=010104" class="c">??</a>|<a href="dis.do?pid=010116" class="c">??</a>|<a href="dis.do?pid=010113" class="c">???</a></li>     <li><a href="dis.do?pid=010119" class="c">??</a>|<a href="dis.do?pid=010120" class="c">???</a>|<a href="dis.do?pid=010132" class="c">??</a></li>     <li><a href="dis.do?pid=010121" class="c">??</a>|<a href="dis.do?pid=010122" class="c">??</a>|<a href="dis.do?pid=010106" class="c">??</a></li>     <li><a href="dis.do?pid=010125" class="c">??</a>|<a href="dis.do?pid=010130" class="c">??</a>|<a href="dis.do?pid=010127" class="c">??</a></li>     <li><a href="dis.do?pid=010109" class="c">??</a>|<a href="dis.do?pid=010133" class="c">??</a>|<a href="dis.do?pid=010124" class="c">??</a></li>     <li><a href="dis.do?pid=010111" class="c">??</a>|<a href="dis.do?pid=010123" class="c">??</a>|<a href="dis.do?pid=010112" class="c">??</a></li>     <li><a href="dis.do?pid=010102" class="c">??</a>|<a href="dis.do?pid=010117" class="c">??</a>|<a href="dis.do?pid=010114" class="c">??</a></li>     <li><a href="dis.do?pid=010115" class="c">??</a>|<a href="dis.do?pid=010118" class="c">??</a>|<a href="dis.do?pid=010134" class="c">??</a></li>     <li><a href="dis.do?pid=010105" class="c">??</a>|<a href="dis.do?pid=010128" class="c">??</a>|<a href="dis.do?pid=010108" class="c">??</a></li>     <li><a href="dis.do?pid=010110" class="c">??</a>|<a href="dis.do?pid=010107" class="c">??</a>|<a href="dis.do?pid=010131" class="c">??</a></li>     <li><a href="dis.do?pid=010103" class="c">??</a></li>

因为当时区域没有修改,导致中间有些问号,其实中间部分主要是一些地方区的中文名称。

可以看出都还是比较有规律的。可用的几个函数都在以上列出,其中LR11.5中最新加的一个函数regexp最大的特点是可以用正则表达式,但我还是习惯用web_reg_save_param_ex ,因为LB/IC 和RB/IC可以来根据边界匹配,二者在实际中运用可能还是会有差别,可能会出现有些情况只能用其中一种才能解决的问题,之前在做上面的时候就曾遇到过用正则表达式匹配出的值会多一些。像上面返回中有还有一行

Html代码 复制代码 收藏代码
  1. <SPAN style="FONT-FAMILY: 'Microsoft YaHei',微软雅黑,SimHei,tahoma,arial,helvetica,sans-serif"><div align="center"><a href="#" class="c">?????????</a></div></SPAN>
<div align="center"><a href="#" class="c">?????????</a></div>

如果用正则表达式就很难区分以上标签中的内容与实际要参数化的内容了。当然这里还是可以的,因为中文长度不一致,可能通过正则表达式限制长度解决,但如果长度也与要获取的参数相同又该怎处理呢?

因为获取上面的参数可以用如下关联函数解决

C代码 复制代码 收藏代码
  1. <SPAN style="FONT-FAMILY: 'Microsoft YaHei',微软雅黑,SimHei,tahoma,arial,helvetica,sans-serif"> web_reg_save_param_regexp(
  2. "paramName=DistrictsName",
  3. "RegExp=class=\"c\">(.{4,8})</a>\r\n", /*match Chinese character use ([\u4e00-\u9fa5]{4,6}).. because chinese character display inproper here */
  4. "Ordinal=all",
  5. LAST);
  6. '这里通过限制长度还是能够正确获取的'</SPAN>  

你可能感兴趣的:(mobile)