php curl,链接标签提取

时间:2010-07-01 23:46:41

标签: php curl

我有提取链接的代码,但我也需要链接标签。我需要存储在数组和链接标签中的链接存储在另一个数组中。

例如,如果网站bbc.com包含代码<a href="bbc.com/sports.html>sports</a>,我需要$linklabel[0]=sports$link[0]=bbc.com/sports.html

代码如下,但错误发生在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;
    }

?> 

2 个答案:

答案 0 :(得分:2)

使用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);
    }

答案 1 :(得分:0)

你来对了地方。请删除您的电子邮件,因为这是共享社区资源,而不是您的个人Q / A计算机。

所以你应该使用simple_html_dom来解析链接。然后它变得像

一样简单
$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;
}