简单php采集网页部分文字问题,求指点,多谢 本帖最后由 hi_5461 于 2013-06-09 15:54:50 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 preg_match_all('#<td class="center_td" height="30">(\d+)</td>\s*<td class="center_td">(.+)</td>\s*<td>(.+)</td>\s*<td>(.+)</td>#isU',$s,$m);print_r($m); 不知道我调用的对不对,但是输出结果中出现了我不想要的字符。Array( [0] => Array ( [0] => <td class="center_td" height="30">1</td> <td class="center_td">2013-02-0711:16</td> <td> 沈阳市永安路</td> <td> 违停,驾驶人不在现场,妨碍其他车辆行人通行的</td> [1] => <td class="center_td" height="30">2</td> <td class="center_td">2013-03-0808:34</td> <td> 沈阳市三好街文萃路由南向北</td> <td> 通过路口遇停止信号时,停在停止线以内或路口内的</td> [2] => <td class="center_td" height="30">3</td> <td class="center_td">2013-03-2210:16</td> <td> 沈阳市三好街文萃路4号</td> <td> 违停,驾驶人不在现场,妨碍其他车辆行人通行的</td> [3] => <td class="center_td" height="30">4</td> <td class="center_td">2013-05-1413:53</td> <td> 沈阳市永安路</td> <td> 违停,驾驶人不在现场,妨碍其他车辆行人通行的</td> [4] => <td class="center_td" height="30">5</td> <td class="center_td">2013-05-3113:29</td> <td> 沈阳市五里河街</td> <td> 违停,驾驶人不在现场,妨碍其他车辆行人通行的</td> ) [1] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) [2] => Array ( [0] => 2013-02-0711:16 [1] => 2013-03-0808:34 [2] => 2013-03-2210:16 [3] => 2013-05-1413:53 [4] => 2013-05-3113:29 ) [3] => Array ( [0] => 沈阳市永安路 [1] => 沈阳市三好街文萃路由南向北 [2] => 沈阳市三好街文萃路4号 [3] => 沈阳市永安路 [4] => 沈阳市五里河街 ) [4] => Array ( [0] => 违停,驾驶人不在现场,妨碍其他车辆行人通行的 [1] => 通过路口遇停止信号时,停在停止线以内或路口内的 [2] => 违停,驾驶人不在现场,妨碍其他车辆行人通行的 [3] => 违停,驾驶人不在现场,妨碍其他车辆行人通行的 [4] => 违停,驾驶人不在现场,妨碍其他车辆行人通行的 ))我是这样调用的:$s = $p->post($remote_server,$post_string);preg_match_all('#<td class="center_td" height="30">(\d+)</td>\s*<td class="center_td">(.+)</td>\s*<td>(.+)</td>\s*<td>(.+)</td>#isU',$s,$m);print_r($m); $s = $p->post($remote_server,$post_string);preg_match_all('#<td class="center_td" height="30">(\d+)</td>\s*<td class="center_td">(.+)</td>\s*<td>(?: )*(.+)</td>\s*<td>(?: )*(.+)</td>#isU',$s,$m);print_r($m);$m 的 1 到 4 下标依次表示为 序号、时间、地点、内容 preg_match_all('#<td class="center_td" height="30">(\d+)</td>\s*<td class="center_td">(.+)</td>\s*<td> (.+)</td>\s*<td> (.+)</td>#isU',$s,$m); 这回正确了还有个小问题,就是我得到的$m有时会是空数组,没有值,这个怎么能判断一下呢?因为不能正确得到结果时我需要重新执行一遍查询,谢谢!以下是返回空数组时的结果:Array( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) [3] => Array ( ) [4] => Array ( )) 为空时你的数据不是上面那种格式吧,你判断$m[0] 是否为空数组就可以了。if(empty($m[0])){ 重新执行一遍查询} 让你说着了,为空时,不是上面的格式,正则会出错,呵呵,所以判断要在正则之前了。应该判断$s里是否有“验证码输入有误,请重新输入!”,但不知道怎么写?<td colspan="2"> 验证码输入有误,请重新输入! </td> 出错时会出现上面那个提示是吧?if(strpos($s,'验证码输入有误,请重新输入!') !==false){ .....重新执行一遍查询....} 又遇到个需要正则的地方,我不会写,帮我看下吧,多谢了目标页面地址:http://218.25.58.44/searchsys/需要采集的数据是:“数据最后更新至2013年6月9日”求规则! 目标在183行,下面是这个页面的源码:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><base href="http://218.25.58.44:80/" /><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>交警查询</title><link href="style/style.css" rel="stylesheet" type="text/css" /><script>function trim(val) { var str = val+""; if (str.length == 0) return str; var re = /^\s*/; str = str.replace(re,''); re = /\s*$/; return str.replace(re,'');}function chkForm1(form) { if ((trim(form("wzcx.autono").value) == "") || (trim(form("wzcx.autono").value).length < 7)) { alert("请输入有效的车牌号!"); form("wzcx.autono").focus(); return false; } if ((trim(form("wzcx.memo").value) == "") || (trim(form("wzcx.memo").value).length < 4)) { alert("请输入车辆识别代码后四位!"); form("wzcx.memo").focus(); return false; } if (trim(form("rand").value) == "") { alert("请输入验证码!"); form("rand").focus(); return false; }}function chkForm2(form) { if (trim(form.cardno.value) == "") { alert("请输入有效的档案编号!"); form.cardno.focus(); return false; } if (trim(form.idno.value) == "") { //alert("请输入有效的证件号码!"); alert("请输入有效的身份证号码!"); form.idno.focus(); return false; } if (trim(form.rand2.value) == "") { alert("请输入验证码!"); form.rand2.focus(); return false; }}</script></head><body><div class="top"><div class="menu"><ul><li><a href="#">网站首页</a></li><li> 沈阳交警</li><li> 交通新闻 </li><li> 视频播放 </li><li> 为您服务 </li><li> 在线办公 </li><li> 信息查询 </li><li> 征求意见 </li><li> 联系我们</ul></div><div class="txt"><!-- 沈阳小雨转小到中雨21℃~14℃ 今天是:2010年5月28日 -->返回首页 / 设为首页 / 添加搜藏</div></div><div class="main"><table width="100%%" border="0" cellspacing="0" cellpadding="0"> <tr> <td valign="top" class="lmenu" > <div class="l1"> <h1><a href="#"><img src="images/cxx_04.png" border="0" /></a></h1> <ul> <li>业务指南</li> <li>表格下载<br />机动车驾驶人身体条件证<br />机动车驾驶申请表…</li> <li>自助报名</li> <li>模拟考场</li> <li>电子地图</li> <li>网上车管所</li> </ul> </div> <div class="l1 l2"> <h1><a href="#"><img src="images/cxx_10.png" border="0" /></a></h1> <ul> <li>业务指南</li> <li>表格下载<br />机动车驾驶人身体条件证<br />机动车驾驶申请表…</li> <li>自助报名</li> </ul> </div> <div class="l1 l3"> <h1><a href="#"><img src="images/cxx_12.png" border="0" /></a></h1> <ul> <li>业务指南</li> <li>表格下载<br />机动车驾驶人身体条件证<br />机动车驾驶申请表…</li> <li>自助报名</li> <li>模拟考场</li> <li>电子地图</li> <li>网上车管所</li> </ul> </div> <div class="l1 l4"> <h1><a href="#"><img src="images/cxx_14.png" border="0" /></a></h1> <div class="video"><img src="images/cxx_15.png" /></div> <div class="video"><img src="images/cxx_16.png" /></div> </div> <div class="kjc"> <img src="images/cxx_17.png" /> </div> </td> <td valign="top"> <div class="content"> <p> </p> <table width="100%%" height="101" border="0" cellpadding="3" cellspacing="0"> <!-- 电子警察拍照信息 --> <tr> <td width="78" height="44"> <b>电子警察拍照信息</b></td> </tr> <form action="search/wzcxrs.action" method="post" name="wfxxForm" id="wfxxForm" onsubmit="return chkForm1(this);"> <tr> <td height="44"><table width="100%" border="0" cellspacing="3" cellpadding="1"> <tr> <td>车型 <select name="wzcx.autotype" id="wzcx.autotype" style="width:90px;"> <option value="小型汽车">小型汽车</option> <option value="大型汽车">大型汽车</option> <option value="外籍汽车">外籍汽车</option> <option value="临时行驶车">临时行驶车</option> <option value="挂车">挂车</option> <option value="原农机号牌">原农机号牌</option> <option value="农用运输车">农用运输车</option> </select></td> <td>车牌号 <input name="wzcx.autono" type="text" class="input" id="wzcx.autono" size="8" maxlength="20" value="辽A" onkeyup="this.value=this.value.toUpperCase();" /></td> <td>车辆识别代码后四位 <input name="wzcx.memo" type="text" class="input" id="wzcx.memo" size="5" maxlength="4" onkeyup="this.value=this.value.toUpperCase();" /></td> <td>验证码<span style="font-size:18px; font-weight:bold; color:#CA0906; "> <input name="rand" type="text" class="input" id="rand" size="5" maxlength="4" /> <img id="img1" src="/searchsys/img.jsp" width="55" height="23" border="0" onclick="javascript:this.src='/searchsys/img.jsp';" alt="看不清,请点击" style="cursor:hand;"/></span></td> <td><input type="image" src="images/cxx_06.png" width="78" height="24" /></td> </tr></table> <br /></td> </tr> </form> <!-- 驾驶员积分信息 --> <tr> <td height="43"> <b>驾驶员信息</b></td> </tr> <form action="search/jsyxxrs.action" method="post" name="jsyxxForm" id="jsyxxForm" onsubmit="return chkForm2(this);"> <tr> <td height="43"><table width="100%" border="0" cellspacing="3" cellpadding="1"> <tr> <td><!--证件类型-->档案编号 <!--<select name="idtype" id="idtype" style="width:100px;"> <option value="1">档案编号</option> <option value="2">身份证号</option> </select>--> <input name="cardno" type="text" class="input" id="cardno" size="18" maxlength="50" /></td> <td><!--号码-->身份证号码 <input name="idno" type="text" class="input" id="idno" size="22" maxlength="50" /></td> <td>验证码<span style="font-size:18px; font-weight:bold; color:#CA0906; "> <input name="rand2" type="text" class="input" id="rand2" size="5" maxlength="4" /> <img src="/searchsys/img2.jsp" width="55" height="23" border="0" onclick="javascript:this.src='/searchsys/img2.jsp';" alt="看不清,请点击" style="cursor:hand;"/> </span></td> <td><input type="image" src="images/cxx_06.png" width="78" height="24" /></td> </tr> </table> <br /></td> </tr> </form> </table> <div> <br/><center> <!--img src="sm.gif" width="531" height="239" /--> </center><br/> <hr/><br/> <center>数据最后更新至2013年6月9日</center> </div> <!-- 按按 -- <table width="100%%" height="76" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="121"><!-- <img src="images/cxx_07.png" width="121" height="24" /> --></td> <td width="176"><!-- <img src="images/cxx_08.png" width="176" height="24" /> --></td> <td width="176"><!-- <img src="images/cxx_09.png" width="133" height="24" /> --></td> <td align="right"></td> </tr> </table> </div> </td> </tr></table></div><div class="foot"><p>友情连接:<select name=""> <option>所属单位</option></select> <select name="select"> <option>所属单位</option> </select> <select name="select2"> <option>所属单位</option> </select> <select name="select3"> <option>所属单位</option> </select> <select name="select4"> <option>所属单位</option> </select></p>Copyright 2009 www.sygajj.gov.cn All Rights Reserved<br />版权所有:沈阳市交通警察支队 E-maill:[email protected] 辽ICP备07501079号 邮编:110003<br /><script src="http://s11.cnzz.com/stat.php?id=2228263&web_id=2228263" language="JavaScript"></script></div></body></html> preg_match('#<center>(.+?)</center>#',$s,$m);echo $m[1]; 求助,php图片压缩的问题 伪静态页面的制作 eclipse 工具问题 付费建站&长期兼职合作!【仅限深圳本地】 如何实现二维数组替换 问一个正则的问题,替换文字 如何加密通过URL打开的文档文件. 我下载了FCKeditor2.0[show only this release],但运行PHP的例子出错,只能运行JAVASCRIPT的? SSH谁有? 关于数据记录分页显示的问题 PDO插入数据的问题 测试XXTea算法,网上没有一个php版本能还原我的字符串,太奇葩了。
print_r($m);
(
[0] => Array
(
[0] => <td class="center_td" height="30">1</td>
<td class="center_td">2013-02-0711:16</td>
<td> 沈阳市永安路</td>
<td> 违停,驾驶人不在现场,妨碍其他车辆行人通行的</td>
[1] => <td class="center_td" height="30">2</td>
<td class="center_td">2013-03-0808:34</td>
<td> 沈阳市三好街文萃路由南向北</td>
<td> 通过路口遇停止信号时,停在停止线以内或路口内的</td>
[2] => <td class="center_td" height="30">3</td>
<td class="center_td">2013-03-2210:16</td>
<td> 沈阳市三好街文萃路4号</td>
<td> 违停,驾驶人不在现场,妨碍其他车辆行人通行的</td>
[3] => <td class="center_td" height="30">4</td>
<td class="center_td">2013-05-1413:53</td>
<td> 沈阳市永安路</td>
<td> 违停,驾驶人不在现场,妨碍其他车辆行人通行的</td>
[4] => <td class="center_td" height="30">5</td>
<td class="center_td">2013-05-3113:29</td>
<td> 沈阳市五里河街</td>
<td> 违停,驾驶人不在现场,妨碍其他车辆行人通行的</td>
) [1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
) [2] => Array
(
[0] => 2013-02-0711:16
[1] => 2013-03-0808:34
[2] => 2013-03-2210:16
[3] => 2013-05-1413:53
[4] => 2013-05-3113:29
) [3] => Array
(
[0] => 沈阳市永安路
[1] => 沈阳市三好街文萃路由南向北
[2] => 沈阳市三好街文萃路4号
[3] => 沈阳市永安路
[4] => 沈阳市五里河街
) [4] => Array
(
[0] => 违停,驾驶人不在现场,妨碍其他车辆行人通行的
[1] => 通过路口遇停止信号时,停在停止线以内或路口内的
[2] => 违停,驾驶人不在现场,妨碍其他车辆行人通行的
[3] => 违停,驾驶人不在现场,妨碍其他车辆行人通行的
[4] => 违停,驾驶人不在现场,妨碍其他车辆行人通行的
))我是这样调用的:$s = $p->post($remote_server,$post_string);
preg_match_all('#<td class="center_td" height="30">(\d+)</td>\s*<td class="center_td">(.+)</td>\s*<td>(.+)</td>\s*<td>(.+)</td>#isU',$s,$m);
print_r($m);
preg_match_all('#<td class="center_td" height="30">(\d+)</td>\s*<td class="center_td">(.+)</td>\s*<td>(?: )*(.+)</td>\s*<td>(?: )*(.+)</td>#isU',$s,$m);
print_r($m);
$m 的 1 到 4 下标依次表示为 序号、时间、地点、内容
还有个小问题,就是我得到的$m有时会是空数组,没有值,这个怎么能判断一下呢?因为不能正确得到结果时我需要重新执行一遍查询,谢谢!以下是返回空数组时的结果:Array
(
[0] => Array
(
) [1] => Array
(
) [2] => Array
(
) [3] => Array
(
) [4] => Array
(
))
if(empty($m[0])){
重新执行一遍查询
}
验证码输入有误,请重新输入!
</td>
.....重新执行一遍查询....
}
目标页面地址:http://218.25.58.44/searchsys/
需要采集的数据是:“数据最后更新至2013年6月9日”
求规则!
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="http://218.25.58.44:80/" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>交警查询</title>
<link href="style/style.css" rel="stylesheet" type="text/css" />
<script>
function trim(val) {
var str = val+"";
if (str.length == 0) return str;
var re = /^\s*/;
str = str.replace(re,'');
re = /\s*$/;
return str.replace(re,'');
}
function chkForm1(form) {
if ((trim(form("wzcx.autono").value) == "") || (trim(form("wzcx.autono").value).length < 7)) {
alert("请输入有效的车牌号!");
form("wzcx.autono").focus();
return false;
}
if ((trim(form("wzcx.memo").value) == "") || (trim(form("wzcx.memo").value).length < 4)) {
alert("请输入车辆识别代码后四位!");
form("wzcx.memo").focus();
return false;
}
if (trim(form("rand").value) == "") {
alert("请输入验证码!");
form("rand").focus();
return false;
}
}
function chkForm2(form) {
if (trim(form.cardno.value) == "") {
alert("请输入有效的档案编号!");
form.cardno.focus();
return false;
}
if (trim(form.idno.value) == "") {
//alert("请输入有效的证件号码!");
alert("请输入有效的身份证号码!");
form.idno.focus();
return false;
}
if (trim(form.rand2.value) == "") {
alert("请输入验证码!");
form.rand2.focus();
return false;
}
}
</script>
</head><body>
<div class="top">
<div class="menu"><ul><li><a href="#">网站首页</a></li><li> 沈阳交警</li><li> 交通新闻 </li><li> 视频播放 </li><li> 为您服务 </li><li> 在线办公 </li><li> 信息查询 </li><li> 征求意见 </li><li> 联系我们</ul>
</div>
<div class="txt"><!-- 沈阳小雨转小到中雨21℃~14℃ 今天是:2010年5月28日 -->返回首页 / 设为首页 / 添加搜藏</div>
</div>
<div class="main">
<table width="100%%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" class="lmenu" >
<div class="l1">
<h1><a href="#"><img src="images/cxx_04.png" border="0" /></a></h1>
<ul>
<li>业务指南</li>
<li>表格下载<br />
机动车驾驶人身体条件证<br />
机动车驾驶申请表…</li>
<li>自助报名</li>
<li>模拟考场</li>
<li>电子地图</li>
<li>网上车管所</li>
</ul>
</div>
<div class="l1 l2">
<h1><a href="#"><img src="images/cxx_10.png" border="0" /></a></h1>
<ul>
<li>业务指南</li>
<li>表格下载<br />
机动车驾驶人身体条件证<br />
机动车驾驶申请表…</li>
<li>自助报名</li>
</ul>
</div>
<div class="l1 l3">
<h1><a href="#"><img src="images/cxx_12.png" border="0" /></a></h1>
<ul>
<li>业务指南</li>
<li>表格下载<br />
机动车驾驶人身体条件证<br />
机动车驾驶申请表…</li>
<li>自助报名</li>
<li>模拟考场</li>
<li>电子地图</li>
<li>网上车管所</li>
</ul>
</div>
<div class="l1 l4">
<h1><a href="#"><img src="images/cxx_14.png" border="0" /></a></h1>
<div class="video"><img src="images/cxx_15.png" /></div>
<div class="video"><img src="images/cxx_16.png" /></div>
</div>
<div class="kjc">
<img src="images/cxx_17.png" />
</div>
</td>
<td valign="top">
<div class="content"> <p> </p>
<table width="100%%" height="101" border="0" cellpadding="3" cellspacing="0">
<!-- 电子警察拍照信息 -->
<tr>
<td width="78" height="44">
<b>电子警察拍照信息</b></td>
</tr>
<form action="search/wzcxrs.action" method="post" name="wfxxForm" id="wfxxForm" onsubmit="return chkForm1(this);">
<tr>
<td height="44"><table width="100%" border="0" cellspacing="3" cellpadding="1">
<tr>
<td>车型
<select name="wzcx.autotype" id="wzcx.autotype" style="width:90px;">
<option value="小型汽车">小型汽车</option>
<option value="大型汽车">大型汽车</option>
<option value="外籍汽车">外籍汽车</option>
<option value="临时行驶车">临时行驶车</option>
<option value="挂车">挂车</option>
<option value="原农机号牌">原农机号牌</option>
<option value="农用运输车">农用运输车</option>
</select></td>
<td>车牌号
<input name="wzcx.autono" type="text" class="input" id="wzcx.autono" size="8" maxlength="20" value="辽A" onkeyup="this.value=this.value.toUpperCase();" /></td>
<td>车辆识别代码后四位
<input name="wzcx.memo" type="text" class="input" id="wzcx.memo" size="5" maxlength="4" onkeyup="this.value=this.value.toUpperCase();" /></td>
<td>验证码<span style="font-size:18px; font-weight:bold; color:#CA0906; ">
<input name="rand" type="text" class="input" id="rand" size="5" maxlength="4" />
<img id="img1" src="/searchsys/img.jsp" width="55" height="23" border="0" onclick="javascript:this.src='/searchsys/img.jsp';" alt="看不清,请点击" style="cursor:hand;"/></span></td>
<td><input type="image" src="images/cxx_06.png" width="78" height="24" /></td>
</tr>
</table>
<br /></td>
</tr>
</form>
<!-- 驾驶员积分信息 -->
<tr>
<td height="43">
<b>驾驶员信息</b></td>
</tr>
<form action="search/jsyxxrs.action" method="post" name="jsyxxForm" id="jsyxxForm" onsubmit="return chkForm2(this);">
<tr>
<td height="43"><table width="100%" border="0" cellspacing="3" cellpadding="1">
<tr>
<td><!--证件类型-->档案编号
<!--<select name="idtype" id="idtype" style="width:100px;">
<option value="1">档案编号</option>
<option value="2">身份证号</option>
</select>-->
<input name="cardno" type="text" class="input" id="cardno" size="18" maxlength="50" /></td>
<td><!--号码-->身份证号码
<input name="idno" type="text" class="input" id="idno" size="22" maxlength="50" /></td>
<td>验证码<span style="font-size:18px; font-weight:bold; color:#CA0906; ">
<input name="rand2" type="text" class="input" id="rand2" size="5" maxlength="4" />
<img src="/searchsys/img2.jsp" width="55" height="23" border="0" onclick="javascript:this.src='/searchsys/img2.jsp';" alt="看不清,请点击" style="cursor:hand;"/> </span></td>
<td><input type="image" src="images/cxx_06.png" width="78" height="24" /></td>
</tr>
</table>
<br /></td>
</tr>
</form>
</table>
<div>
<br/><center>
<!--img src="sm.gif" width="531" height="239" /--> </center><br/>
<hr/><br/>
<center>数据最后更新至2013年6月9日</center>
</div> <!-- 按按 --
<table width="100%%" height="76" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="121"><!-- <img src="images/cxx_07.png" width="121" height="24" /> --></td>
<td width="176"><!-- <img src="images/cxx_08.png" width="176" height="24" /> --></td>
<td width="176"><!-- <img src="images/cxx_09.png" width="133" height="24" /> --></td>
<td align="right"></td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</div>
<div class="foot">
<p>友情连接:<select name="">
<option>所属单位</option>
</select>
<select name="select">
<option>所属单位</option>
</select>
<select name="select2">
<option>所属单位</option>
</select>
<select name="select3">
<option>所属单位</option>
</select>
<select name="select4">
<option>所属单位</option>
</select>
</p>
Copyright 2009 www.sygajj.gov.cn All Rights Reserved<br />版权所有:沈阳市交通警察支队 E-maill:[email protected] 辽ICP备07501079号 邮编:110003<br /><script src="http://s11.cnzz.com/stat.php?id=2228263&web_id=2228263" language="JavaScript"></script>
</div>
</body>
</html>
echo $m[1];