Jump to content

[SOLVED] Simple Scraper... Weird Output


phoenixx

Recommended Posts

Okay, maybe I just need a Blue Monster and some sleep, but.... :wtf:

 

I'm scraping a ringtone site just so that I can download all of the ringtones and add them to my personal library.

 

The website's code that I am targeting is:

<tr><td><a href="/ringtone/527783/"><img src="/img/icon/ringt.jpg" border=0>Jackson 5                      - Who's Loving You </a> </td><td align=center><a href="/ringtones/classical/" class=cat_link>Classical</a></td><td align=center><img src="/img/rating/star0.gif" border=0></td><td align="right" class=smgrey2>5 months ago</td><td align="center" class=smgrey2><span class="b">13895</span></td><td align="right"><span class="b"><a href="/profile/stambaugh01">stambaugh01</a></span></td></tr>

 

I would like for it to output the actual filename which in this case would be 527783.  I would also like for it to output the title of the file.

 

There are about 50 or so listings per page, and would like for it to automatically go to the next page to scrape.

 

Here is my code:

<?
$data = @file_get_contents("http://www.XXXXXXXXXXXX.com/ringtones/classical/");

preg_match_all('/href="\/ringtone\/.*?\<img src="\/img\/icon\/ringt.jpg" border=0>([^"]*).*?\/"><img src="\/img\/icon\/.*?border=0>([^"]*)<\/td><td align=center>/is',$data,$out);
// preg_match_all('/href="\/ringtone\/.*?\<img src="\/img\/icon\/ringt.jpg" border=0>([^"]*).*?\/"><img src="\/img\/icon\/.*?border=0>([^"]*)<\/td><td align=center>/is',$data,$out);
	if ((isset($out[1]) && isset($out[2])) === FALSE) {	 // Let's do some error checking to see if there is data to insert into the database.  If not let's end the script
		break;
	}
	$d = array_combine($out[1], $out[2]);
	 // End Error Checking
		foreach($d as $k=>$v){
			echo $k . " --- " . $v . "<br> ";
		}
?>

 

The output is skipping and only outputting the title of every other row, but now directory name.

 

Thanks in advance for the help.

Link to comment
Share on other sites

A way of doing it:

 

<?php
$page = 1;
$ids = array();
while (true) {
$html = file_get_contents('http://www.mytinyphone.com/ringtones/classical/?page_ring=' . $page++);
$match_count = preg_match_all('~href="/ringtone/([0-9]+)/~i', $html, $matches);
if ($match_count > 0) {
	$ids = array_merge($ids, $matches[1]);
} else {
	//page doesn't exist
	break;
}
}
echo '<pre>' . print_r($ids, true) . '</pre>';
?>

 

Will load all pages though, and thus probably time out, but should work with the appropriate settings (assuming the website in question doesn't cut you off due to too many requests).

 

I'm not too sure about this, but maybe it could be optimized by loading all the page sources into a single string first, and then run a single preg_match_all() on the huge string. Don't know if it'll be more efficient.

Link to comment
Share on other sites

Addition: Forgot to grab the titles. Although my for loop isn't that elegant.

 

<?php
$page = 1;
$data = array();
while (true) {
$html = file_get_contents('http://www.mytinyphone.com/ringtones/classical/?page_ring=' . $page++);
$match_count = preg_match_all('~href="/ringtone/([0-9]+)/"><img[^>]*>(.*?)</a>~is', $html, $matches);
if ($match_count > 0) {
	for ($i = 0; $i < $match_count; $i++) {
		$data[] = array($matches[1][$i], $matches[2][$i]);
	}
} else {
	//page doesn't exist
	break;
}
}
echo '<pre>' . print_r($data, true) . '</pre>';
?>

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.