PHP Crawl Sina News program code

Source: Internet
Author: User


First, you need to download a simple_html_dom third-party extension library, the specific download and use details can be viewed: the use of simple_html_dom.

The support of the file_get_contents () function and curl is required for the environment, with the specific code as follows:

<?php  function timingimg ($url, $dirname, $interval) {  ignore_user_abort ();  //Browser closed, Program continues to execute   set_time_limit (0);//Ignore Default 30 sec timeout    $interval  = empty ($interval)? 3600:$

Interval    $dirname  = empty ($dirname)? '

news/': $dirname;   if (Empty ($url)) {    die (' Please enter destination URL!!!

');

&NBSP;&NBSP}   do{     $arr  = loadimg ($url, $dirname);

 return  $arr     //Returns the path  sleep ($interval) saved by the crawl content;

 die;

  }while (TRUE);    /** * Get all news links * Parameters   $url    News list Address * parameters   $dirname   News picture save path *  return   two-dimensional array &n

bsp;  All News Records * * FUNCTION&NBSP;LOADIMG ($url, $dirname) {    set_time_limit (0);

  include_once (' simple_html_dom.php ');

   $ch  = curl_init ();

  curl_setopt ($ch, Curlopt_url, $url);   curl_setopt ($ch, Curlopt_heaDer,false);

  curl_setopt ($ch, curlopt_returntransfer,1);

   $output  = curl_exec ($ch);

  curl_close ($ch);

   $html  = new simple_html_dom ();

   $html->load ($output);

   $images  = array ();

   $arr  = array ();   foreach ($html->find (' li  a ')  as  $element) {      if (  preg_match (' #^http:\/\/tech\.sina\.com\.cn\/it\/[\d]{4}\-[\d]{1,2}\-[\d]{1,2}\/[\d]+\.shtml$ #i ', $

Element->href)) {   array_push ($images, $element->href);

&NBSP;&NBSP}  }    $images  = array_unique ($images);

     sort ($images);   for ($i =0 $i <10; $i + +) {  //Crawl only 10 records     $arr [] = getcontent ($

images[$i], $dirname); &NBSP}   return  $arr; }  /** * * *  Crawl Single news content * Parameters    $url   News Page address * parameters & nbsp;  $dirname   Picture Save path * Return   a news record   one-dimensional array/function getcontent ($url, $dirname) { include_

Once (' simple_html_dom.php ');

  $html  = new simple_html_dom ();

  $data  = file_get_contents ($url);

  $html->load ($data);        $arr  = array ();     foreach ($html->find (' h1#artibodytitle ')  as  $element) {   

  $arr [' title ']=  @iconv (' GBK ', ' utf-8 ',  $element->innertext);

;

&NBSP;&NBSP;&NBSP}     $str  =  ';    foreach ($html->find (' div#artibody p ')  as  $element) {      $STR .=  $element;      -  &nbsp}     $arr ['

Content '] =  $str;    foreach ($html->find (' div.img_wrapper img ')  as  $element) {        $arr [' Alt '] = $element->alt;

       $data  = file_get_contents ($element->src);        $info  = getimagesize ($element->src);//get image  Information       switch ($info [2]) {       

 case 1:         $str  =  ' gif ';

       break; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CASE&NBSP;2:         $str

 =  ' jpg ';

       break;         case 3:         $str

 =  ' PNG ';

       break;

        default:        continue;

       break;     &NBSP;&NBSP}        $filename  = time (). Rand (1,999999). $str;        if (!is_dir ($dirname)) {      

  mkdir ($dirname, 0777,true); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}        $fp  = fopen ($dirname. $ FileName, ' W ');                 

  fwrite ($fp, $data);

      fclose ($FP);

       $arr [' img '] =  $dirname. $filename;

          &nbsp    return  $arr; }?>

If you have better suggestions, please come forward

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.