简单的HTML DOM查找标签并从页面链接中获取数据

时间:2019-01-23 15:55:43

标签: php simple-html-dom

简单的HTML DOM查找标签并从页面链接中获取数据 嗨,我是Simple HTML DOM,基本上我需要从h2标题和内容 链接(page / id / 1)。我要堆栈的重点是从page获取数据。 格式应与

相同
  1. 标题

竞争表lik1, 来自link5的内容

  1. 标题2

链接中的内容, 来自2的内容

   <section class="level">
       <h2> title </h2>
       <a class="links" href="page/id/1">link1 </a>
       <a class="links" href="page/id/2">link2 </a>
       <a class="links" href="page/id/3">link3 </a>
       <a class="links" href="page/id/4">link4 </a>
       <a class="links" href="page/id/5">link5 </a>
   </section>
   <section class="level">
       <h2> title 2 </h2>
       <a class="links" href="page/id/7">link1 </a>
       <a class="links" href="page/id/8">link2 </a>
   </section>
   <section class="level">
       <h2> title 3  </h2>
       <a class="links" href="page/id/9">link2 </a>
       <a class="links" href="page/id/10">link3 </a>
   </section>

我知道任何帮助人员都应该遵循这些原则

   foreach ($html->find('h2') as $key => $value) { 
       echo $html->find('h2',0)->plaintext;
       //this is where Im stack getting the data from the link 
       foreach ( ) {
           echo data from the link example.com/page.php/id/1 
           echo data from the link example.com/page.php/id/2 
       }      
   }

1 个答案:

答案 0 :(得分:0)

您可以使用<section>以类名level find find('section[class=level]') find('section[class=level] a')然后例如可以循环子节点并检查nodeName。

仅获取锚点,可以使用$html = new simple_html_dom(); $html->load($data); $result = $html->find('section[class=level]'); foreach ($result as $item) { foreach($item->childNodes() as $childNode) { if ($childNode->nodeName() === "h2") { echo $childNode->innertext . "<br>"; } if ($childNode->nodeName() === "a") { echo $childNode->getAttribute("href") . "<br>"; } } }

例如:

 title 
page/id/1
page/id/2
page/id/3
page/id/4
page/id/5
 title 2 
page/id/7
page/id/8
 title 3  
page/id/9
page/id/10

结果

{{1}}