首页 文章

php curl,链接标签提取

提问于
浏览
1

我的拼贴项目最近两天我有代码提取链接,但我也需要链接标签 . 我需要链接存储在一个数组和链接标签存储在另一个数组中 . 例如,如果网站bbc.com有代码运动,我需要$ linklabel [0] = sports和$ link [0] = bbc.com / sports.html .

代码如下,但是出现致命错误:在第14行的C:\ wamp \ www \ test \ d.php中调用未定义的方法DOMXPath :: find()
] *?> . *?@ si '); // Strip out javascript $var = preg_replace($search, 910295 , 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.'
'; } function fread_url($url) { if(function_exists( 910298 )){ $ch = curl_init(); $user_agent = 910299 . 910300 ; $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; //打印网页 . curl_close($ CH); } else {$ hfile = fopen($ url,"r"); if($ hfile){while(!feof($ hfile)){$ html . = fgets($ hfile,1024);返回$ html; }?>

2 回答

  • 1

    这很容易使用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);
        }
    
  • 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;
    }
    

相关问题