1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > php抓取网页上的指定内容

php抓取网页上的指定内容

时间:2022-08-18 14:04:52

相关推荐

php抓取网页上的指定内容

<?php

//ignore_user_abort(true);

//set_time_limit(0);

//获取所有的政府机关和事业单位

$url = "/company/news.php";

$contents = file_get_contents($url);

//如果出现中文乱码使用下面代码

$getcontent = iconv("gb2312", "utf-8",$contents);

echo $contents;

//exit;

$mode = "/ <ul class=\"newslist\">(.*)<div id=\"right\">/is";

preg_match_all($mode,$contents,$matches);

print_r($matches);

//exit;

$xinwen_str=$matches[0][0];

//echo $xinwen_str;

$xinwen_arr1=explode("<li>",$xinwen_str);

foreach($xinwen_arr1 as $key=>$value){

$xinwen_arr2=explode("</li>",$xinwen_arr1[$key]);

$xinwen_arr1[$key]=$xinwen_arr2[0];

$xinwen_arr1[$key]=str_replace("\n","",$xinwen_arr1[$key]);

$xinwen_arr1[$key]=str_replace("\r","",$xinwen_arr1[$key]);

}

//print_r($xinwen_arr1);

for($j=1;$j<count($xinwen_arr1);$j++){

$mode1 = "/(.*)<a/is";

preg_match_all($mode1,$xinwen_arr1[$j],$matches1);

$riqi_str=$matches1[0][0];

$riqi_arr=explode(" ",$riqi_str);

$riqi=$riqi_arr[0];//获取到日期

echo $riqi."<br>";

$mode2= "/>(.*)<\/a>/isU";

preg_match_all($mode2,$xinwen_arr1[$j],$matches2);

$title_str=$matches2[0][0];

$title_arr=explode(">",$title_str);

$title=str_replace("</a","",$title_arr[1]);//获取到名称

echo $title."<br>";

$lianjie_arr=explode("\"",$xinwen_arr1[$j]);

$lianjie=$lianjie_arr[1];

$a=strstr($lianjie,"http");

if($a=="") $lianjie="".$lianjie;

echo $lianjie."<br>";

}

?>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。