duangong937906 2014-05-19 09:27
浏览 72
已采纳

解析dom xpath什么都不做

I want to get all the links on a page so I could get attributes like title of the a href etc...

<?php
function exception_handler($exception) {
  echo "Uncaught exception: " , $exception->getMessage(), "
";
}

set_exception_handler('exception_handler');

function dom_create()
{
  echo("domcreate");
  $file = file_get_html('http://www.facebook.com/plugins/fan.php?connections=100&id=40796308305');
  echo($file);
  $doc = new DOMDocument();
  $doc->loadHTMLFile($file);

  $xpath = new DOMXpath($doc);

  $elements = $xpath->query("//*[@id]");


  if (!is_null($elements)) {
    foreach($elements as $e){
      $documentLinks = $e->getElementsByTagName('a');
    }
    else
      echo "NULL";
  }
}

dom_create();
?>

I don't get any output even if I just set echo statements. Anyone an idea?

  • 写回答

2条回答 默认 最新

  • duandingqi9442 2014-05-19 10:02
    关注

    I solved it by doing it via get_contents and give it a context.

    <?php
    function exception_handler($exception) {
      echo "Uncaught exception: " , $exception->getMessage(), "
    ";
    }
    
    set_exception_handler('exception_handler');
    
    function dom_create()
    {
      $context = stream_context_create(array('http' => array('header' => 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0')));
      $file = file_get_contents('http://www.facebook.com/plugins/fan.php?connections=100&id=6568341043637',false, $context);
      $dom = new DOMDocument;
      $dom->loadHTML($file);
      foreach ($dom->getElementsByTagName('a') as $node) {
        echo $dom->saveHtml($node), PHP_EOL;
      }
    }
    
    dom_create();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统