使用php从网页中提取特定数据

时间:2017-01-04 04:55:15

标签: php html

我想在发布新通知时为我的工作网站创建一个PHP脚本,因此请关注页面网址

http://www.mahapwd.com/nit/ueviewnotice.asp?noticeid=1767

从这个页面我想要一个变量 Date&会议时间(日期和时间分别为两个变量) 会议地点发布在 请帮我创建一个完美的PHP脚本。

我尝试创建以下脚本,但它会产生很多错误

<?php
$url1 = "http://www.mahapwd.com/nit/ueIndex.asp?district=12";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
preg_match("/href=(.*)\", $data, $urldata);
$url2 = "http://www.mahapwd.com/nit/$urldata[1];
curl_setopt($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data2 = curl_exec($ch);
preg_match("/Published On:</b>(.*)<\/font>", $data, $pubDt);
$PubDate = $pubDt[1];
preg_match("/Time of Meeting:</b>(.*)&nbsp", $data, $MtDt);
$MeetDate = $MtDt[1];
preg_match("/Time of Meeting:</b>$MtDt[1]&nbsp(.*)</font>", $data, $MtTime);
$MeetTime = $MtTime[1];
preg_match("/Place of Meeting:</b>(.*)<\/font>", $data, $pubDt);
$PubDate = $pubDt[1];
?>

1 个答案:

答案 0 :(得分:1)

您好我已经为您完成了简单的代码。您可以从http://simplehtmldom.sourceforge.net/

下载simple_html_dom.php
 require_once "simple_html_dom.php";
 $url='http://www.mahapwd.com/nit/ueviewnotice.asp?noticeid=1767';


//parse url
for ($i=0;$i<1;$i++) {
$html1 = file_get_html($url);
if(!$html1){  echo "no content";    }
else {
//here is parsed html 
$string1 = $html1;

//now you need to find table
  $element1=$html1->find('table'); 
  //here is a table you need
 $input=$element1[2];
 //now you can select row from here 
foreach($input->find('td') as $element) {

  //in here you can find name than save it to database than check it
  } 


 }

}
相关问题