韦德国际_韦德国际1946官方网站_韦德国际1946手机版
做最好的网站

php中抓取网页内容的实例详解,php中抓取网页内

日期:2019-07-03编辑作者:韦德国际1946官方网站

php中抓取网页内容的实例详解

php中抓取网页内容的实例详解,抓取网页内容

php中抓取网页内容的实例详解

方法一:

使用file_get_php中抓取网页内容的实例详解,php中抓取网页内容的代码。contents方法完结

  $url = "http://news.sina.com.cn/c/nd/2016-10-23/doc-ifxwztru6951143.shtml";
    $html = file_get_contents($url);
    //如果出现中文乱码使用下面代码
    //$getcontent = iconv("gb2312", "utf-8",$html);
    echo "<textarea style='width:800px;height:600px;'>".$html."</textarea>";

代码一点也不细略,一看就懂,不表达了。

 方法二:

使用curl实现

$url = "http://news.sina.com.cn/c/nd/2016-10-23/doc-ifxwztru6951143.shtml";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$html = curl_exec($ch);
curl_close($ch);

echo "<textarea style='width:800px;height:600px;'>".$html."</textarea>";

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

增加那句代码,表示假诺央浼被重定向时,能够访问到结尾的央浼页面,不然伏乞的结果会显得如下内容:

<head><title>Object moved</title></head>
<body><h1>Object Moved</h1>This object may be found <a href="some link." rel="external nofollow" >here</a>.</body>

满意疑问请留言也许到本站社区调换座谈,多谢阅读,希望能帮助到大家,感激我们对本站的支撑!

php中抓取网页内容的实例详解 方法一: 使用file_get_contents方法达成 $url = "...

1.比照所抓取网页的平整去编写

方法一:

方法一:

public function comment(){
    for ($i = 700; $i <750; $i  ){
        $aa = $this->caiji($i);
        var_dump($i);
    }

}

private function caiji($id){

    $v  = 'http://www.xx.com'.$id;
    $info = file_get_contents($url); // 原网址

    $ch = curl_init();
    $timeout = 5;
    curl_setopt ($ch, CURLOPT_URL, $v);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $content = curl_exec($ch);
    $content = mb_convert_encoding($content, 'utf-8', 'GBK,UTF-8,ASCII');

    $preg = "/<tr.*?>(.*?)</tr>/ism"; // 这里是表达式,大神看看
    preg_match_all($preg,$content,$matches);
    unset($matches['0']['0']);
    unset($matches['0']['1']);
    //$aa = $matches['0'];
    if($matches['0']){
        $dd = '';
        $cc = '';
        foreach ($matches['0'] as $key => $value) {
            preg_match_all("/<td.*?>(.*?)</td>/ism",$value,$aa);
            $dd[] = $aa;
        }

        //var_dump($dd);

        foreach ($dd as $vv) {
                $cc['s_id'] = $vv['1']['1'];
                $cc['pid'] = $vv['1']['2'];
                $cc['uid'] = $vv['1']['3'];
                $cc['name'] = $vv['1']['4'];
                $cc['car_type'] = $vv['1']['5'];
                $cc['mobile'] = $vv['1']['6'];
                $cc['zhan_id'] = $vv['1']['7'];
                $cc['city'] = $vv['1']['8'];
                $cc['pay'] = $vv['1']['9'];
                $cc['title'] = $vv['1']['10'];
                $cc['comment'] = $vv['1']['11'];
                $cc['time'] = $vv['1']['12'];

                $aa = M('Comment_cc')->add($cc);
                //var_dump($cc);exit;
        }
    }

}

使用file_get_contents方法实现

使用file_get_contents方法实现

2.php 获取文摘

        $url = "http://news.sina.com.cn/c/nd/2016-10-23/doc-ifxwztru6951143.shtml";
        $html = file_get_contents($url);
        //如果出现中文乱码使用下面代码
        //$getcontent = iconv("gb2312", "utf-8",$html);
        echo "<textarea style='width:800px;height:600px;'>".$html."</textarea>";

本文由韦德国际发布于韦德国际1946官方网站,转载请注明出处:php中抓取网页内容的实例详解,php中抓取网页内

关键词: PHP 扩展类 伟德体育app最