Jump to content

Screen Scraping


Lytheum

Recommended Posts

I need to pull some info off a certain website that is setup like this:

 

<b>Blah1:</b> 11<br/>
<b>Blah2:</b> 22<br/>
<b>Blah3:</b> 33<br/>
<b>Blah4:</b> 44<br/>
<b>Blah5:</b> 55<br/>

 

So I should be able to try:

 

echo "$blah1"; //value of 11
echo "blah5"; //value of 55

 

My code which doesn't work at all, hopefully somebody can improve on it:

 

// Get page
$url = "http://url.url.url";
$data = implode("", file($url)); 

// Get content items
preg_match_all ("/<\/b>([^`]*?)<br\/>/", $data, $matches);

// Loop through each content item
foreach ($matches[0] as $match) {
// First, get title
preg_match ("/<b>Blah1:<\/b>([^`]*?)<br\/>/", $match, $temp);
$title = $temp['1'];
$title = strip_tags($title);
$title = trim($title);
}
echo "$blah1";

 

Thanks for any future help, if you still don't understand lemme know.

Link to comment
Share on other sites

Try:

 

<?php
$str="<b>Blah1:</b> 11<br/>
<b>Blah2:</b> 22<br/>
<b>Blah3:</b> 33<br/>
<b>Blah4:</b> 44<br/>
<b>Blah5:</b> 55<br/>";
preg_match_all('|Blah([0-9]).*?([0-9]+)|',$str,$matches);
$blah = array();
foreach($matches[1] as $k => $v){
$blah[$v] = $matches[2][$k];
}
echo $blah[1]; //11
echo $blah[5]; //55
?>

Link to comment
Share on other sites

Awesome that does work. Now just one more question if you could.

 

How do I do it when there is no pattern to follow? For instance:

 

<?php
$str="<b>blah:</b> 11<br/>
<b>blah:</b> 22<br/>
<b>blah:</b> 33<br/>
<b>blah:</b> 44<br/>
<b>blah:</b> 55<br/>
";
?>

Link to comment
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.