Jump to content

Multi URL Metatag Scraper


young_coder

Recommended Posts

Dear all,

Can somebody help me to modify this script to scrape more than just one page?

Thank you very much advance.

 

 

<?php

$url = "http://www.example.com";

$fp = fopen( $url, ‘r’ );

$content = "";

 

while( !feof( $fp ) ) {

$buffer = trim( fgets( $fp, 4096 ) );

$content .= $buffer;

}

 

$start = '<title>';

$end = '<\/title>';

 

preg_match( "/$start(.*)$end/s", $content, $match );

$title = $match[ 1 ];

$metatagarray = get_meta_tags( $url );

$keywords = $metatagarray[ "keywords" ];

$description = $metatagarray[ "description" ]; 

 

echo "<div><strong>URL:</strong> $url</div>\n";

echo "<div><strong>Title:</strong> $title</div>\n";

echo "<div><strong>Description:</strong> $description</div>\n";

echo "<div><strong>Keywords:</strong> $keywords</div>\n";

?>

Link to comment
Share on other sites

Put the URLs you want to scrape in an array and loop through the array. Also if you use file_get_contents, your script will look cleaner:

<?php
$urls = array('http://www.example.com/','http://www.example.org/','http://www.example.net/');
$start = '<title>';
$end = '<\/title>';
foreach ($urls as $url) {
$content = file_get_contents($url);
preg_match( "/$start(.*)$end/s", $content, $match );
$title = $match[1];
$metatagarray = get_meta_tags($url);
$keywords = $metatagarray['keywords'];
$description = $metatagarray['description'];

echo "<div><span style='font-weight:bold'>URL:</span> $url</div>\n";
echo "<div><span style='font-weight:bold'>Title:</span> $title</div>\n";
echo "<div><span style='font-weight:bold'>Description:</span> $description</div>\n";
echo "<div><span style='font-weight:bold'>Keywords:</span> $keywords</div>\n";
}
?>

 

Ken

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.