Capture the title, content, and source image code of an article using the CI framework

Source: Internet
Author: User
This is a code for capturing pages and backing up the pages for future calls. you can view the code and share it with friends. If you have any comments, log on and leave a message. this is a code that captures the page and makes a backup so that you can call it later. you can check the code again, so you can share it with friends. Post-logon messages with comments
Public function snatch () {set_time_limit (0); $ this-> benchmark-> mark ('code _ start '); /* get second-hand car news of different categories */for ($ I = 1; $ I <= 4; $ I ++) {$ url = 'http: // news.2sche.cn/list.asp? Stype = '. $ I; $ result = $ this-> curl_snatch ($ url); preg_match_all ('/\ D \/(.*?) <\/Strong>/', $ result, $ page_news); // print_r ($ page_news); // echo ''; /* obtain the news list of all pages in a single category */for ($ j = 1; $ j <= $ page_news [1]; $ j ++) {if (1 = $ j) {$ url_news = 'http: // news.2sche.cn/list.asp? Stype = '. $ I;} else {$ url_news = 'http: // news.2sche.cn/list.asp? Page = '. $ j.' & stype = '. $ I;} $ result_news = $ this-> curl_snatch ($ url_news); preg_match_all ('/.*? <\/A> <\/td>/sim ', $ result_news, $ url_newslist); // print_r ($ url_newslist ); /* traverse each url on the list page */foreach ($ url_newslist [1] as $ url_newslists) {$ url_newsinfo = 'http: // news.2sche.cn/'.w.url_newslists; $ result_newsinfo = $ this-> curl_snatch ($ url_newsinfo);/* obtain the title */preg_match_all ('/(.*?) <\/Strong> <\/h3>/sim ', $ result_newsinfo, $ title); // print_r ($ title [1]); /* obtain the source */preg_match_all ('/[Source :(.*?) ] <\/Span> <\/td>/sim ', $ result_newsinfo, $ source); // print_r ($ source [1]); /* get content */preg_match_all ('/(.*?) <\/Td>/sim ', $ result_newsinfo, $ content); // print_r ($ content [1] [0]); /* get all image URLs * // preg_match_all ('// sim', $ content [1] [0], $ img ); preg_match_all ('// sim', $ content [1] [0], $ img); // echo 'ddddd '; // print_r ($ img [1]); // echo 'dddd
'; // Exit; $ picture = ''; foreach ($ img [1] as $ imgs) {// echo $ imgs; // echo'
'; If (strpos ($ imgs, 'http: //') ===false) {continue;} $ img_source = file_get_contents ($ imgs ); /* obtain the name of a single image */$ img_names = trim (strrchr ($ imgs, '/'), '/'); // print_r ($ img_name ); // echo $ img_names; // exit; $ picture. = $ img_names. ':'; file_put_contents (". /static/uploads/news /". $ img_names, $ img_source); // replace the image path with $ img_path = '/static/uploads/news /'. $ img_names; $ content [1] [0] = str_replace ($ imgs, $ img_path, $ content [1] [0]);} // print_r ($ picture ); // echo 'hhhh
'; // Print_r ($ content [1] [0]); // echo'
'; $ Data = array ('title' => $ title [1] [0], 'source' => $ source [1] [0], 'contents' => trim ($ content [1] [0]), 'Picture '=> $ picture, 'Style' => $ I, 'create _ time' => time (),); if (! $ This-> News_model-> add ($ data) {continue;} // print_r ($ data); exit;} echo '';}} $ this-> benchmark-> mark ('code _ end'); echo $ this-> benchmark-> elapsed_time ('code _ start', 'Code _ end ');} function curl_snatch ($ url =' http://www.2sche.cn/buy.asp ') {$ Url = trim ($ url); $ content = ''; if (extension_loaded ('curl') {$ ch = curl_init (); // 2. set options, including URL curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1 ); curl_setopt ($ ch, CURLOPT_HEADER, 0); // 3. execute and get the HTML document content $ output = curl_exec ($ ch); $ content = iconv ("GBK", "UTF-8", $ output ); if ($ output = FALSE) {echo "cURL Error :". curl_error ($ ch);} // $ info = curl_getinfo ($ ch); // echo 'get '. $ info ['URL']. 'consumed time '. $ info ['total _ time']. 'second'; // 4. release curl handle curl_close ($ ch);} else {$ content = file_get_contents ($ url);} return trim ($ content );}


Author: Li Jiashun

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.