Duber
Duber

Reputation: 145

Count # of links programmatically

Is there a way I can programmatically count the number of links for a website? Does google provide an api that can I can programmatically query?

Upvotes: 2

Views: 259

Answers (3)

murze
murze

Reputation: 4103

Using the Zend Framework you can use find all the links on a webpage with this piece of code:

$numberOfLinks = 0
$client = New Zend_Http_Client();
$client->setUri('http://www.yoururl.com');
$response = $client->request();
if ($response->isSuccessful()) {
    $body = $response->getBody();
    $doc = Zend_Search_Lucene_Document_Html::loadHTML($body,TRUE)
    $links = $doc->getLinks();
    foreach ($links as $link) {
         $numberOfLinks++;
    }
}

The result is obviously stored in $numberOfLinks :-)

Upvotes: 0

Mike Gensel
Mike Gensel

Reputation: 11

Maybe using the Google Ajax Search API? I'm not very familiar with it so I have no code examples but you can go here to check it out: http://code.google.com/apis/ajaxsearch/

There are also some php code examples in the documentation

Upvotes: 0

Amy B
Amy B

Reputation: 17977

You can write a scraper (I dont recommend it though).

$page = file_get_contents('http://www.google.com/?q=link:site.com');
$page = str_replace(array('<b>', '</b>', ','), array('', '', ''), $page);

preg_match('/Results (\d+) - (\d+) of about (\d+) for/', $page, $match);

var_dump($match);

Upvotes: 1

Related Questions