简单的html dom解析器获取没有标记的文本

时间:2013-03-11 03:25:13

标签: web-scraping web-crawler simple-html-dom

我想使用PHP使用简单的HTML DOM解析器解析HTML对象。我要提取的特定部分未正确包装在任何标记中。

<li class="tags">
   Required text: <span itemprop="testCat"><a href="/topics/new-topic/index.html" title="New Topic" onclick="s_objectID=&quot;http://www.example.com/topics/new-topic/index.html_1&quot;;return this.s_oc?this.s_oc(e):true">New Topic</a></span>, <span itemprop="testCat"><a href="/topics/new-topic-2/index.html" title="New Topic" onclick="s_objectID=&quot;http://www.example.com/topics/new-topic-2/index.html_1&quot;;return this.s_oc?this.s_oc(e):true">New Topic</a></span>, <span itemprop="testCat"><a href="/topics/new-topic-3/index.html" title="New Topic 3" onclick="s_objectID=&quot;http://www.example.com/topics/new-topic-3/index.html_1&quot;;return this.s_oc?this.s_oc(e):true">New Topic 3</a></span>, 
   <div class="more">
      <a href="javascript: void(0);" class="more-trigger" onclick="s_objectID=&quot;javascript: void(0);_1&quot;;return this.s_oc?this.s_oc(e):true">more</a>
      <div class="more-tags" style="top: 15px; left: 0px; display: none;">
         <div class="hd"></div>
         <div class="bd">
            <ul id="topic-filedin">
               <li>Another Required Text :
                  <a href="/topics/new-topic-4/index.html" onclick="s_objectID=&quot;http://www.example.com/topics/new-topic-4/index.html_1&quot;;return this.s_oc?this.s_oc(e):true">New Topic 4</a>
               </li>
               <li>Topic Intended For :
                  <a href="/topics/for-kids/index.html" onclick="s_objectID=&quot;http://www.example.com/topics/for-kids/index.html_1&quot;;return this.s_oc?this.s_oc(e):true">For Kids</a>
               </li>
            </ul>
         </div>
         <div class="ft"></div>
      </div>
      <script type="text/javascript">
         SNI.Node.ArticleInfo.moreTags();
      </script> 
   </div>
</li>

我可以使用

将文本包含在标记内
$categories = $single_content->find('li[class=tags] span');
foreach ($categories as $key) {
  echo $key->plaintext . '<br>';
}

我无法找到Required textAnother Required Text以及Topic Intended For

1 个答案:

答案 0 :(得分:0)

获取“另一个必需文本”和“用于主题的主题”您可以使用此 -

$ text = $ single_content-&gt; find(“ui [class = topic-filedin] / li”);

$ textArray = array();

foreach($ text as $ ta)

{

$textArray[] = $ta->plaintext;

}

您将获得数组中所需的文本。