使用Microsoft JScript解析HTML [英] Parsing HTML using Microsoft JScript

查看:68
本文介绍了使用Microsoft JScript解析HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Windows Scripting Host中的JScript收集有关网页列表的信息.为此,我需要获取每个HTML页面的源代码并进行解析.

我需要的是,使用responseText/responseXML 创建某种HTML DOM对象,并在该HTML DOM上使用熟悉的getElementByIdgetElementsByTagNames函数.我该怎么办?

我正在使用以下JScript,并使用Windows Scripting Host V5.8运行它.

I''m trying to collect information about a list of web pages using JScript in Windows Scripting Host. And to do that, I need to get the source of each HTML page and parse it.

What I need is, create some kind of HTML DOM object using the responseText/responseXML and use the familiar getElementById, and getElementsByTagNames functions on that HTML DOM. How do I do that?

I am using the following JScript and running it using Windows Scripting Host V5.8.

var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {
            var xmldom = xmlhttp.responseXML;
            
            if (xmldom.parsed) {
                var imgs = xmldom.getElementsByTagName('img');
                WScript.Echo("imgs: " + imgs + " " + imgs.length);
            }
        }
    }
}

xmlhttp.open("GET", "http://www.example.com/somepage.php", false);
xmlhttp.send();



我得到以下输出.



And I am getting the following output.

imgs:  0





I have been searching the whole night yesterday and had no luck!

推荐答案

好吧,您找回了HTML而不是XML,这就是为什么这样的经验.

在这里看看: HTML到DOM [ http://forums.mozillazine.org/viewtopic.php?f= 19& t = 1594275 [ ^ ]
Well, you got back a HTML and not a XML and that is the reason why you are having such experience.

Have a look here: HTML to DOM[^]

Following discussion would also help: http://forums.mozillazine.org/viewtopic.php?f=19&t=1594275[^]


这篇关于使用Microsoft JScript解析HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆