Php crawls page content

Source: Internet
Author: User
Tags preg
Php crawls the page content first to capture a useful link in the page, then captures the content of the link on the page, and uses the for loop, but an error occurs when the loop reaches the second time,
After thinking for a long time, I don't know where the problem is. please take a look.
$ Url = 'http: // www.meishij.net/chufang/diy /? Page = 1 # listnav '; $ opts = array ('http' => array ('User _ agent' => "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0) ",); $ context = stream_context_create ($ opts); $ con = file_get_contents (" $ url ", false, $ context); // entry $ preg = '#(.*)# '; Preg_match_all ($ preg, $ con, $ arr); // assign the search content to the array for ($ I = 0; $ I <20; $ I ++) // capture a single page {// print_r ($ arr [0] [$ I]); $ ss = $ arr [2] [$ I]; echo $ ss; echo"
"; $ Opts = array ('http' => array ('User _ agent' =>" Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0 )",)); $ context = stream_context_create ($ opts); $ cons = file_get_contents ("$ ss", false, $ context); // Title $ preg = '#(. *) # '; preg_match_all ($ preg, $ cons, $ arr); // assign the search content to the array print_r ($ arr [0] [0]); // content $ preg = '#

(.*)(.*)

# '; Preg_match_all ($ preg, $ cons, $ arr); // assign the search content to the array print_r ($ arr [0] [0]); echo"
"; Print_r ($ arr [0] [1]); echo"
"; Print_r ($ arr [0] [2]); echo"
"; // Image $ preg = '#

# '; Preg_match_all ($ preg, $ cons, $ arr); // assign the search content to the array print_r ($ arr [0] [0]); echo"
"; Print_r ($ arr [0] [1]); echo"
"; Print_r ($ arr [0] [2]); echo"
"; Print_r ($ arr [0] [3]); echo"
"; Print_r ($ arr [0] [4]); echo"
"; Print_r ($ arr [0] [5]); echo"
"; Print_r ($ arr [0] [6]);}


Reply to discussion (solution)

Which of the following Experts knows how to tell 3Q,

Solve the problem.

Print_r $ arr; // check the content of $ arr.

Preg_match_all ($ preg, $ con, $ arr); // assign the search content to the array

For ($ I = 0; $ I <20; $ I ++) // capture a single page
{

// Print_r ($ arr [0] [$ I]);
$ Ss = $ arr [2] [$ I];
Echo $ ss;
Echo"
";

$ Opts = array (
'Http' => array (
'User _ agent' => "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0 )",
)
);
$ Context = stream_context_create ($ opts );
$ Cons = file_get_contents ("$ ss", false, $ context );
Print_r ($ cons );
Die ();
// Title
$ Preg = '#(.*)#';
Preg_match_all ($ preg, $ cons, $ arr); // assign the search content to the array
Print_r ($ arr [0] [0]);

// Content
$ Preg = '#

(.*)(.*)

#';
Preg_match_all ($ preg, $ cons, $ arr); // assign the search content to the array
Print_r ($ arr [0] [0]);
Echo"
";


Looking at the red part above, the array names inside and outside the loop are the same. Is there no problem?

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.