是否可以使用QueryPath选择HTML注释?

时间:2011-08-12 12:05:17

标签: html html-parsing querypath

我认为这可以使用jQuery,但是如何在QueryPath中完成?

Selecting HTML Comments with jQuery

如果没有,是否有人可以建议可以选择评论的HTML解析器?

2 个答案:

答案 0 :(得分:2)

QueryPath附带了一个名为QPXML的扩展,它有几个附加方法。其中之一是comment()

要使用它,只需将其包含在您的脚本中:

include 'QueryPath/QueryPath.php';
include 'QueryPath/Extensions/QPXML.php';

htmlqp($html, $selector)->comment();

这将检索附加到当前所选节点的第一条注释。

如果你在同一个节点中有一组非常复杂的评论,你可以这样做:

$nodes = $qp->get();
foreach ($nodes as $node) {
   foreach ($node->childNodes as $child) {
      if ($child->nodeType == XML_COMMENT_NODE) {
         // $child is a comment.
         print $child->textContent;
      }
   }
}

这有点丑陋,但它可以更好地访问一个元素中有很多注释的情况。

答案 1 :(得分:0)

通过querypath获取HTML页面的所有评论:

#include <syscall.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char* argv[])
{
    const int cmd1 = system("sleep 5");
    int out_fd = creat("out.txt",0644);
    int err_fd = creat("err.txt",0644);


    if(out_fd < 0){
        //write(2, "Can't open file out.txt", strlen("Can't open file out.txt"));
        dup2(err_fd, 2);
        write(2, "Can't open file out.txt \n", strlen("Can't open file out.txt \n"));

        return -1;
        }else if(err_fd < 0){
            //write(2, "Can't open file err.txt", strlen("Can't open file err.txt"));
            dup2(err_fd, 2);
            write(2, "Can't open file err.txt \n", strlen("Can't open file err.txt \n"));

            return -1;
        }
    if((cmd1 !=(0)))
    {
        //write(2, "There is an error on command1", strlen("There is an error on command1"));
        dup2(err_fd, 2);
        write(2, "Error on command1 \n", strlen("Error on command1 \n"));

        return -1;
        }
    if((cmd1==(0)))
    {

        //write(1, "success on command1", strlen("success on command1"));
        dup2(out_fd, 1);
        write(1, "success on command1 \n", strlen("success on command1 \n"));
        const int cmd2 = system("echo $HOME");;

        if(( cmd2==(0)))
        {

            //write(1, "success on command2", strlen("success on command2"));
            dup2(out_fd, 1);
            write(1, "success on command2 \n", strlen("success on command2 \n"));

        }
        else if(( cmd2 !=(0)))
        {

            //write(2, "There is an error on command2", strlen("There is an error on command2"));
            dup2(err_fd, 2);
            write(2, "Error on command2 \n", strlen("Error on command2 \n"));

        }
    }
return 0;
}