Jump to content

Recommended Posts

I'm trying to get back into PHP, but I'm running into some trouble. I'm creating a little script that scraps data from a page and then displays it elsewhere. (I'm actually going to insert it into a database, but for now, just trying to display it! There's two pages I'm scraping from.

 

This scrapes the list of players from one web page:

$html = file_get_contents("http://www.somesite.com/players.php?search=who");

preg_match_all(
    '/<td width=\'33\.3333333333\%\'><a href=\'\/players.php\?lookup=.*?\'>(.*?)<\/a>/',
    $html,
    $players,
    PREG_SET_ORDER
);

That matches fine. Then for each name I want to scrape another page that has more information about each individual player:

foreach ($players as $player) {

$html2 = file_get_contents("http://www.somesite.com/players.php?lookup=$player");

preg_match_all(
    '/<font size=\'\+1\'><strong>.*?<\/font><\/strong>/',
    $html2,
    $longName,
PREG_SET_ORDER
);
preg_match_all(
    '/Race<\/font><\/td><td><font size=\'\-1\'>.*?<\/font>/',
    $html2,
    $race,
PREG_SET_ORDER
);
preg_match_all(
    '/Citizenship<\/font><\/td><td><font size=\'\-1\'>.*?<\/font>/',
    $html2,
    $citizenship,
PREG_SET_ORDER
);
preg_match_all(
    '/Level<\/font><\/td><td><font size=\'\-1\'>.*?<\/font>/',
    $html2,
    $level,
PREG_SET_ORDER
);

echo $longName[0][0];
echo ("<br>");
$rac = implode("", $race[0]);
$newrace = substr($rac, 4, -1);
echo $newrace;
echo ("<br>");
$cit = implode("", $citizenship[0]);
$newcit = substr($cit, 11, -1);
echo $newcit;
echo ("<br>");
$lev = implode("", $level[0]);
$newlevel = substr($lev, 5, -1);
echo $newlevel;
echo ("<br>");

}

This does not work. I get Warning: implode() [function.implode]: Invalid arguments passed in /my/path/names.php on line <three implode lines> and I get NO echos except for the break lines. HOWEVER, if I just use this code outside of a loop, i.e. I just do $html2 = file_get_contents("http://www.somesite.com/players.php?lookup=MyPlayerName"); it works perfectly.

 

I'm newb at coding, so I can't even figure out what data it is or is not getting in the iteration, and why it's not displaying/getting it when it's looped through each name as opposed to when it works perfectly when I input a single name in the file_get_contents. Can anyone see errors in how I'm going about this or suggest some checks to put in to help figure out what the heck is going wrong?

 

Thanks much for any help.

Link to comment
https://forums.phpfreaks.com/topic/212938-lost-in-iterating-through-scraped-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.