> php教程 > PHP源码 > 본문

新浪新闻采集程序

PHP中文网
풀어 주다: 2016-05-25 16:58:43
원래의
1164명이 탐색했습니다.

利用正则表达式提取内容信息

<?php
    //error_reporting(0);
    $date=date("Ymd");
    //echo $date;
    $url="http://top.finance.sina.com.cn/ws/GetTopDataList.php?top_type=day&top_cat=all&top_time=".$date."&top_show_num=20&top_order=ASC";
    $doc=file_get_contents($url);
    header("Content-Type:text/html;charset=gb2312");//UNICODE转GB2312
    //echo $doc;
    preg_match_all(&#39;/"url":".*l/U&#39;,$doc,$article);//获取文章列表
    $article=current($article);//二维数组转一维数组
    //print_r($article);
    $rule="/\"url\":(.*)\"\"/";
    function OnlyUrl($string){
       $string=stripslashes($string);
       $string=str_replace(&#39;"url":"&#39;,"",$string);
       return $string; 
    }
     
    function GetTitle($url){  //获取文章标题
      $doc=file_get_contents($url);
      $rule="/<h1 (.*)<\/h1>/";
      preg_match($rule,$doc,$result);
      echo $result;
    }
    foreach($article as &$value){
        $value=OnlyUrl($value);
    }//提取了URL 
?>
로그인 후 복사
<?php
  function GetTitle($url){  //获取文章标题
    $rule="/<h1 (.*)<\/h1>/";
    preg_match_all($rule,$doc,$result);
    $rule="/>(.*)</";//截掉<h1></h1>
    preg_match($rule,$result[0][1],$title);
    return $title[1];
  }
  function GetContent($url){
    $doc=file_get_contents($url);//打开网页
  // echo $doc; 
    $rule="/publish_helper(.*)publish_helper_end/xs";
    preg_match($rule,$doc,$result);//取出正文部分
    //echo $result[0];
    $rule="/publish_helper(.*)<\/blockquote>/xs";
    preg_match($rule,$result[0],$out);//取出链接部分
    //print_r($out);
    $content=str_replace($out[0],"",$result[0]);//提取纯文字段的内容
    echo $content;
  }
?>
로그인 후 복사
관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!