PHP Crawl page Content

Source: Internet
Author: User
Tags preg
Grab a useful link in a page, crawl the link on the page, use a For loop, but loop to the second time to make a mistake,
Think for a while, do not know the problem where, please the great God to help you look.
$url = ' Http://www.meishij.net/chufang/diy/?page=1#listnav '; $opts = Array (' http ' =>array (' user_agent ' = = "Moz illa/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);    The search content is assigned to the array for ($i =0; $i <20; $i + +)//single-page intercept {//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); The search content is assigned to the array print_r ($arr [0][0]);//Content $preg = ' #

(.*) (.*)

#'; Preg_match_all ($preg, $cons, $arr); The search content is assigned to the array print_r ($arr [0][0]); echo "
";p Rint_r ($arr [0][1]); echo"
";p Rint_r ($arr [0][2]); echo"
";//Picture $preg = ' #

#'; Preg_match_all ($preg, $cons, $arr); The search content is assigned to the array print_r ($arr [0][0]); echo "
";p Rint_r ($arr [0][1]); echo"
";p Rint_r ($arr [0][2]); echo"
";p Rint_r ($arr [0][3]); echo"
";p Rint_r ($arr [0][4]); echo"
";p Rint_r ($arr [0][5]); echo"
";p Rint_r ($arr [0][6]); }


Reply to discussion (solution)

Which master knows to tell, 3Q,

Solve, why the loop does not pass.

Print_r $arr;//Look at $arr what content

Preg_match_all ($preg, $con, $arr); Search content assignment to array

for ($i =0; $i <20; $i + +)//single page intercept
{

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); Search content assignment to array
Print_r ($arr [0][0]);

Content
$preg = ' #

(.*) (.*)

#';
Preg_match_all ($preg, $cons, $arr); Search content assignment to array
Print_r ($arr [0][0]);
echo "
";


Look at the red part above, the name of the array inside and outside the loop is 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.