PHP:抓取网页指定内容

效果图

在这里插入图片描述

代码
<?php
$url = "http://要抓取的网页";
$contents = file_get_contents($url);
//如果出现中文乱码使用下面代码
//$getcontent = iconv("gb2312", "utf-8",$contents);
//echo $contents;
//exit;

$mode = "#<ol class=\"linenums\">(.*)</ol>#is";
preg_match_all($mode,$contents,$matches);
//print_r($matches);
$xinxi_str=$matches[0][0];
//echo $xinxi_str;
$xinxi_arr1=explode("<li ",$xinxi_str);
//print_r($xinxi_arr1);

foreach($xinxi_arr1 as $key=>$value){
    $xinxi_arr2=explode("</li>",$xinxi_arr1[$key]);
    $xinxi_arr1[$key]=$xinxi_arr2[0];
}
//print_r($xinxi_arr1);
//echo count($xinxi_arr1);

echo "<table border='1px' bordercolor='green' cellspacing='0px' ;";
echo "<tr><th>svcname</th><th>status</th>";
for($j=1;$j<count($xinxi_arr1);$j++){
    $mode2= "#>(.*)#is";
    preg_match_all($mode2,$xinxi_arr1[$j],$matches2);
    //print_r($matches2);
    $title_str=$matches2[0][0];
    $title_arr=explode(">",$title_str);
    //print_r($title_arr);
    $title=str_replace("</","",$title_arr[1]);
    if (strpos($title,".default.run"))
    {
        $list_str=str_replace(".default.run="," ",$title_arr[1]);
        $list_arr=explode(" ",$list_str);
        //print_r($list_arr);
        echo"<tr><td>{$list_arr[0]}</td><td>{$list_arr[1]}</td>";
    }
}
echo "<h3>服务信息列表</h3>";
echo "</table>";
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值