douna4762 2014-10-17 12:10
浏览 89

如何使用php crawler从网站获取div标签中包含的所有数据

i'm having a code for simple php crawler that fetches all the html pages from websites upto depth 5 but if ,I run that for getting all the data contained in a div tag with its id like [container, main ,wrapper.etc] then it show unexpected result...heres the php code ::

<?php
    $a=$_POST['t1'];
function crawl_page($url, $depth = 5)
{
  static $seen = array();
  if (isset($seen[$url]) || $depth === 0) {
    return;
  }

  $seen[$url] = true;

  $dom = new DOMDocument('1.0');
  @$dom->loadHTMLFile($url);

  $anchors = $dom->getElementsByTagName('div');
  foreach ($anchors as $element) {
        $href = $element->getAttribute('id');
    //$href = $element->find('div[id=main]', 0)->plaintext;
    if (0 !== strpos($href, 'main')) {

        $host = "http://".parse_url($url,PHP_URL_USER);
        $href = $host. '/' . ltrim($href, '/');
    }
    crawl_page($href, $depth - 1);
  }

  echo "New Page:<br /> ";
  echo "URL:",$url,PHP_EOL,"<br />","CONTENT:",PHP_EOL,$dom->saveHTML(),PHP_EOL,PHP_EOL,"  <br />        <br />";
}

crawl_page($a, 5);
?>

this code is working good for anchor tags but i want this working for div tag only that fetches all the data contained in it nothing else. i want this for my project if anybody has done that then helpme out.......the html code is written down

<HTML>
<head>
<title></title>
</head>
<body>
<form method="POST" action="crawler1edit[2].php">
Enter Url:-<input type="text" name="t1">
<input type="submit" value="send" name="s1">
</form>
</body>
</HTML>

in action attribute crawler1edit[2].php is the php file containing php code written at the top

  • 写回答

1条回答 默认 最新

  • dtr87341 2014-10-17 12:36
    关注

    Is there a reason why you aren't just targeting the divs by ID ?

    $dom->getElementById ("main");
    
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试