Kiran George
Kiran George

Reputation: 101

php curl , link label extraction

I have the code to extract the links, but I need the link label also. I need links to be stored in an array and link label to be stored in another array.

for example if the site bbc.com has the code <a href="bbc.com/sports.html>sports</a>, I need $linklabel[0]=sports and $link[0]=bbc.com/sports.html.

The code is below, but error occurs as Fatal error: Call to undefined method DOMXPath::find() in C:\wamp\www\test\d.php on line 14

<?php
    $url='http://edition.cnn.com/?fbid=4OofUbASN5k';

    $var = fread_url($url);// function calling to get the page from curl
    $search = array('@<script[^>]*?>.*?</script>@si');  // Strip out javascript
    $var = preg_replace($search, "\n", html_entity_decode($var)); // Strip out javascript

    $linklabel = array();
    $link = array();
    $dom = new DOMDocument($var);
    @$dom->loadHTML($var);
    $xpath = new DOMXPath($dom);// Grab the DOM nodes 

foreach($xpath->find('a') as $element)
   {
     array_push($linklabel, $element->innerText);
     print $linklabel;
     array_push($link, $element->href);
     print $link.'<br>';
    }


    function fread_url($url)
    {
        if(function_exists("curl_init")){
            $ch = curl_init();
            $user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; ".
                          "Windows NT 5.0)";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
            curl_setopt( $ch, CURLOPT_HTTPGET, 1 );
            curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
            curl_setopt( $ch, CURLOPT_FOLLOWLOCATION , 1 );
            curl_setopt( $ch, CURLOPT_FOLLOWLOCATION , 1 );
            curl_setopt( $ch, CURLOPT_URL, $url );

            curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
            $html = curl_exec($ch);
            //print $html;//printing the web page.
            curl_close($ch);
        }
        else{
            $hfile = fopen($url,"r");
            if($hfile){
                while(!feof($hfile)){
                    $html.=fgets($hfile,1024);
                }
            }
        }
        return $html;
    }

?> 

Upvotes: 2

Views: 1652

Answers (2)

Byron Whitlock
Byron Whitlock

Reputation: 53861

You have come to the right place. Please remove your email as this is a shared community resource, not your personal Q/A machine.

So You should use simple_html_dom to parse the links. Then it becomes as simple as

$dom = file_get_html('http://www.google.com/');

// get the label of all links. see the docs for searching options
foreach ($dom->find('a') as $links)
{
    $link->innerText;
    $link->href;
}

Upvotes: 0

Unicron
Unicron

Reputation: 7456

This is easy using the Simple HTML DOM.

$html = file_get_html('http://www.google.com/');

$linklabel = array();
$link = array();

foreach($html->find('a') as $element)
   {
     array_push($linklabel, $element->innerText);
     array_push($link, $element->href);
    }

Upvotes: 3

Related Questions