Jump to content

Recommended Posts

Thought it would be simple but ... here's what I'm doing I'm scraping an external page and would like it to go to through the pagination setup on the site I'm scraping which sets a new page for every 100 listings. I have a loop counter that increases by 1 for every loop but here's what I need it to do. If the loop equals 100 or 200 or 300 or 400 or so on and so on, then one of the variables needs to have a value of 100 or 200 or 300, etc...

 

Also I need the loop to stop if it finds no data without dying. Kind of a long code but there is only 3 places where I need help and I've highlighted them with // HELP NEEDED HERE code.

 

Oh, I show loops through 1000, but should just find that it is at end of sites paginated data

 

<?
// Let's Extract Page Links
$loopcount=(1);
WHILE ($loopcount <10001){
$data = @file_get_contents("http://xxxxx.com/$urldirectory/$looppage"); // HELP NEEDED HERE (if EOF then change $urldirectory)
preg_match_all('/<p>.*?<a href="([^"]*)".*?<p>.*?href="([^"]*)"/is',$data,$out);
$d = array_combine($out[1], $out[2]);
foreach($d as $k=>$v){
$loopcount++;
echo $k . "<br>";
if ($loopcount == (100)){
$looppage=("index100.html"); // HELP NEEDED HERE should just be simple lines of code instead of multiples below, right?
}
if ($loopcount == (200)){
$looppage=("index200.html");
}
if ($loopcount == (300)){
$looppage=("index300.html");
}
if ($loopcount == (400)){
$looppage=("index400.html");
}
if ($loopcount == (500)){
$looppage=("index500.html");
}
if ($loopcount == (600)){
$looppage=("index600.html");
}
if ($loopcount == (700)){
$looppage=("index700.html");
}
if ($loopcount == (800)){
$looppage=("index800.html");
}
if ($loopcount == (900)){
$looppage=("index900.html");
}
if ($loopcount == (1000)){
$looppage=("index1000.html");
}
// HELP NEEDED HERE if the scraper page has reached end of pagination then change $urldirectory
$loopcount++;
echo $v . "<br>";
if ($loopcount == (100)){
$looppage=("index100.html");
}
if ($loopcount == (200)){
$looppage=("index200.html");
}
if ($loopcount == (300)){
$looppage=("index300.html");
}
if ($loopcount == (400)){
$looppage=("index400.html");
}
if ($loopcount == (500)){
$looppage=("index500.html");
}
if ($loopcount == (600)){
$looppage=("index600.html");
}
if ($loopcount == (700)){
$looppage=("index700.html");
}
if ($loopcount == (800)){
$looppage=("index800.html");
}
if ($loopcount == (900)){
$looppage=("index900.html");
}
if ($loopcount == (1000)){
$looppage=("index1000.html");
// HELP NEEDED HERE if the scraper page has reached end of pagination then change $urldirectory
}
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/125624-loop-while-the-condition-has-data/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.