模拟post请求抓取网页资源数据,用正则表达式获取有用数据
程序员文章站
2022-07-13 16:10:40
...
最近公司要求做抓取网站资源数据的功能,而且需要将返回的数据进行分类,只返回有用的数据,这就需要用到正则表达式,运用下列代码实现了上述功能,这是个.NET webservice实现的功能,希望对以后有同样需求的兄弟提供帮助。
ps:正则表达式写的不太好,应该会有更好更简洁的方式满足需求,希望大牛赐教。
[WebMethod] public String getResourceSearchMsg(String cityId,String pageNo) { try { WeiXinBase.HttpNetRequest request = new WeiXinBase.HttpNetRequest(); StringBuilder sbuilder = new StringBuilder(); sbuilder.Append("url"); if (cityId != null && !"".Equals(cityId)) { sbuilder.Append("&super_id=" + cityId); } if (pageNo == null || "".Equals(pageNo)) { sbuilder.Append("&dev_page_number = 0"); } else { sbuilder.Append("&dev_page_number = " + pageNo); } request.Url = sbuilder.ToString(); request.Senddata = "super_id=10"; request.RMethod = WeiXinBase.Method.POST; //模拟发送url请求返回的数据 String message = request.PageRequest(); //过滤\n 转换成空 String withoutNString = message.Replace("\n", ""); //过滤\r 转换成空 String withoutRString = withoutNString.Replace("\r", ""); //过滤\t 转换成空 String withoutTString = withoutRString.Replace("\t", ""); //过滤\ 转换成空 String newString = withoutTString.Replace("\\", ""); //获取html中的body标签 String bodyStr = Regex.Match(newString, @"<body.*>.*</body>").ToString(); //过滤注释 String result2 = Regex.Replace(bodyStr, @"<!--(?s).*?-->", "", RegexOptions.IgnoreCase); //过滤nbsp标签 String result3 = Regex.Replace(result2, @" ", "", RegexOptions.IgnoreCase); //获取body中的所有table Regex regex = new Regex(@"<table.*?>[\s\S]*?<\/table>"); MatchCollection mc = regex.Matches(result3); //获取集合类中自己需要的某个table String newHtmlStr = mc[3].ToString(); //将数据返回 //Response.Write(newHtmlStr); return newHtmlStr; } catch (Exception ex) { return ex.Data.ToString(); } }
以下是获取到的html数据,数据进行过删减,红色部分是有用的数据
<table width="80%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="4">
<img src="img/t-l.png" width="4" height="26" />
</td>
<td>
<div class="inquire-t1">
<img src="img/inquire.gif" width="16" height="16" align="absmiddle" />
查询区
</div>
</td>
<td width="4">
<img src="img/t-r.png" width="4" height="26" />
</td>
</tr>
</table>
<div class="inquire-content">
<FORM name="searchForm" action="resource_anticipation_query_bss1.jsp"
method=post>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr height="30">
<td>
<input name="viewNumber" type="text" id="viewNumber" value="" />
<input type="button" name="button1" id="button1" value="装机地址查询" onclick="queryAll();"
/>
</td>
</tr>
</table>
</FORM>
</div>
</div>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top">
<div id="tab">
<div class="Menubox">
<ul>
<li id="one1" onclick="setTab('one',1,3)" class=hover>
分线盒地址
</li>
<li id="one2" onclick="setTab('one',2,3)" class=null>
已有用户地址
</li>
<li id="one3" onclick="setTab('one',3,3)" class=null>
在建社区
</li>
</ul>
</div>
<div class="Contentbox">
<div id="con_one_1" class=hover style=0>
<table width="90%" border="0" cellspacing="0" align="left">
<form name="title1" action="#" method="post">
<input type="hidden" name="super_id" value="10" />
<input id="dev_all_page" type="hidden" name="dev_all_page" value="6128"
/>
</form>
</table>
<div class="clear">
</div>
<hr>
<table width="98%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td align="left" width="5%" height="22">
设备类型
</td>
</tr>
<tr bgcolor="E1FFFF">
<td align="left" id="A_0">
LAN
</td>
</tr>
</table>
</div>
<div id="con_one_2" class=null style=display:none>
<table width="90%" border="0" cellspacing="0" align="left">
<form name="title2" action="#" method="post">
<input type="hidden" name="super_id" value="10" />
<input type="hidden" name="partition_code" value="null" />
<input type="hidden" name="zone_cd" value="020" />
<tr align=left height=25>
<td class="noth" width='23%' align=left>
共819709项 第1 到20项
</td>
</tr>
</form>
</table>
<div class="clear">
</div>
<hr>
<table width="80%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td align="left" width="10%" height="22">
设备类型
</td>
</tr>
</table>
</div>
<div id="con_one_3" class=null style=display:none>
<table width="90%" border="0" cellspacing="0" align="left">
</table>
</div>
</div>
</div>
</td>
</tr>
</table>
上一篇: 模拟post请求抓取网页资源数据,用正则表达式获取有用数据
下一篇: 项目开发中遇到的问题总结