抓取外部网页并获取所有出站链接

时间:2014-01-12 04:36:03

标签: php curl hyperlink outbound

我想要的很简单 获取网页HTML并抓取所有出站链接

到目前为止我所拥有的是

  <?php
    function get_content($URL){
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_URL, $URL);
          $data = curl_exec($ch);
          curl_close($ch);
          return $data;
      }


 $html = get_content('http://example.com');
    ?>

1 个答案:

答案 0 :(得分:1)

利用DOMDocument

$dom = new DOMDocument;
$dom->loadHTML($html); // <----------- Pass the HTML content you retrieved from get_content()
foreach ($dom->getElementsByTagName('a') as $tag) {
         echo $tag->getAttribute('href');
}