Monday, December 3, 2007

morphbank browse scraper

Just in case you would want to add a little www.morphbank.net image scaper to your Web page. It returns the url to embed the image into a Web page and the link back to the morphbank record. Using nifty php client url library functions (curl). Any morphbank image browse query will work. Just find the browse you want and cut and replace the returned url in place of the one below.

$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'http://www.morphbank.net/Browse/ByImage/?tsn=655371');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);$regex = "/Image \[(.+?)\]/";
preg_match_all ($regex,$data,$match);
foreach($match[1] as $id )
echo '<a href="http://morphbank.net/Show/?id='.$id.'"> <img src="http://morphbank.net/Show/?id='.$id.'&imgType=jpg" /></a>';
?>

Works ok for a small number of images, will really bog down a web page

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.