Jump to content

Limit the # of records returned from a scraping session?


Modernvox

Recommended Posts

Can I limit the number of records returned from a scraping session?

 

<?php
$st = isset($_POST['submit']) ? $_POST['state'] : '';

$urls = array();
if ($st == "AL") 
{
$urls = array("http://auburn.craigslist.org", "http://bham.craigslist.org"); 

}
else if ($st == "AK") 
{
$urls= array("http://anchorage.craigslist.org");
}
else if ($st == "AZ") 
{
$urls = array("http://anchorage.craigslist.org");
}

foreach ($urls as $url) {
    $html = file_get_contents("$url/muc/");

    preg_match_all('/<a href="([^"]+)">([^<]+)<\/a><font size="-1">([^"]+)<\/font>/s', $html,$posts,PREG_SET_ORDER);
    //echo "<pre>";print_r($posts);


    foreach ($posts as $post) {

        //print $post[0]; //HTML
        $post[2] = str_ireplace($url,"",$post[2]); //remove domain
        echo "<a href=\"$url{$post[1]}\">{$post[2]}<font size=\"3\">{$post[3]}<br />";
        print "<BR />\n";

    }
}
?>

 

Thanks in Advance

yes.

 

<?php
$i = 1; //set start point;
$limit = 5; //set limit;
foreach ($posts as $post) {
//print $post[0]; //HTML
$post[2] = str_ireplace($url,"",$post[2]); //remove domain
echo "<a href=\"$url{$post[1]}\">{$post[2]}<font size=\"3\">{$post[3]}<br />";
print "<BR />\n";

if ($i == $limit)
{
	break;
}
$i++;
}
?>

 

something like that.

yes.

 

<?php
$i = 1; //set start point;
$limit = 5; //set limit;
foreach ($posts as $post) {
//print $post[0]; //HTML
$post[2] = str_ireplace($url,"",$post[2]); //remove domain
echo "<a href=\"$url{$post[1]}\">{$post[2]}<font size=\"3\">{$post[3]}<br />";
print "<BR />\n";

if ($i == $limit)
{
	break;
}
$i++;
}
?>

 

something like that.

 

Thanks Marcus. Much appreciated.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.