PHP HTML DOM php+ Regular collection of article code

Source: Internet
Author: User
Tags foreach regular expression


<?php
Contains PHP simple HTML Dom class library files
Include_once ('./simplehtmldom/simple_html_dom.php ');
Capturing HTML
function Getwebcontent ($url) {
$ch = Curl_init ();
$timeout = 10;
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_connecttimeout, $timeout);
curl_setopt ($ch, curlopt_followlocation, 1);
$contents = Trim (curl_exec ($ch));
Curl_close ($ch);
return $contents;
}

Get title and URL
$string =
Getwebcontent (' HTTP://WWW.BABYTREE.COM/LEARN/ZHUNBEIHUAIYUN/JIJIBEIYUN/2 ');
Regular match <li> get title and address
Preg_match_all ("/<li><a href="/learn/article/(. *) "> (. *) </a>/",
$string, $out, Preg_set_order);
foreach ($out as $key => $value) {
$article [' title '] [] = $out [$key][2];
$article [' link '] = "http://www.babytree.com/learn/article/". $out [$key][1];
}
Get article content based on URL
foreach ($article [' link '] as $key => $value) {
$html = file_get_html ($value);
$div = $html->find (' div[id=pagenum_0] ');
$article [content][] = $div [0]->innertext;
}
Title Transcoding---Really use this step--because we were going to use UTF8.
I can't save it as a file without transcoding.
foreach ($article [title] as $key => $value) {
$article [title][$key] = iconv (' utf-8 ', ' GBK ', $value);//transcoding
}
Deposit in File
$num = count ($article [' title ']);
for ($i =0; $i < $num; $i + +) {
File_put_contents ("{$article [title][$i]}.txt", $article [' content '] [$i]);
}
* I would have sent it before 12 o'clock. But look down on it all 3:30 ... Even if it was yesterday.
It is the best to get the content of the article, the speed is the fastest,
But the positive is good, but the regular expression is really difficult! So a little check,
There are also a lot of people on the Web using PHP simple Dom Although the efficiency is slow, but the effect is good
From the containing class library file to the write TXT file is about 7/8 seconds and with further optimizations, especially when it comes to getting the content of the article, that's disgusting.
We can do a little research on the * *
?>

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.