我想计算body标签中每个节点的字符数和标签数。之后,将使用此数量的字符和标签来获取每个标签的密度。为了得到正文,我使用PHP Simple HTML DOM Parser中的函数find()。
function FindBody($str) {
// String --> DOM Elements
$string = str_get_html($str);
// Find body
$e = $string->find('body');
file_put_contents('text_1.txt', $e); }
以上代码段将获得以下输出:
<body class="html not-front not-logged-in one-sidebar sidebar-first page-node page-node- page-node-163472 node-type-article page- page- ltr"><div id="skip-link"> <a href="#main-content" class="element-invisible element-focusable">Skip to main content</a>..... </body>
每次从链接获取HTML时,正文都会有所不同。现在,我被困住了。我不知道如何迭代/递归获取每个标签。我在某处读过,可以递归完成,但需要花费很多时间。
我需要使用DOM,因为我正在遵循这个算法。