java - 如何在解析 html 页面时从 html 页面中的 javascript 函数中提取变量的值

标签 java javascript html html-parsing jsoup

我必须解析一个 html 页面。我必须提取下面 html 中分配给 JavaScript 函数的 name 元素的值。我如何使用 JSoup 来做到这一点。

<input type="hidden" name="fields.DEPTID.value"/>

JS:

departmentId.onChange = function(value) {                                       
    var departmentId = dijit.byId("departmentId");

    if (value == null || value == "") {
        document.transferForm.elements["fields.DEPTID.value"].value = "";
        document.transferForm.elements["fields.DEPTID_DESC.value"].value = "";
    } else {
        document.transferForm.elements["fields.DEPTID.value"].value = value;
        document.transferForm.elements["fields.DEPTID_DESC.value"].value = departmentId.getDisplayedValue();

        var locationID = departmentId.store.getValue(departmentId.item, "loctID");
        var locationDesc = departmentId.store.getValue(departmentId.item, "loct");

        locationComboBox = dijit.byId("locationId");

        if (locationComboBox != null) {
            if (locationID != "") {
                setLocationComboBox(locationID, locationDesc);
            } else {
                setLocationComboBox("AMFL", "AMFL - AMY FLORIDA");
            }
        }
    }
};

最佳答案

我会尝试从顶部教你:

//Connect to the url, and get its source html
Document doc = Jsoup.connect("url").get();

//Get ALL the elements in the page that meet the query
//you passed as parameter. 
//I'm querying for all the script tags that have the
//name attribute inside it
Elements elems = doc.select("script[name]");

//That Elements variable is a collection of
//Element. So now, you'll loop through it, and
//get all the stuff you're looking for
for (Element elem : elems) {
    String name = elem.attr("name");

    //Now you have the name attribute
    //Use it to whatever you need.
}

现在,如果您需要 Jsoup 查询方面的帮助来获取您可能需要的任何其他元素,请参阅 API 文档来帮助:Jsoup selector API

希望有帮助 =)

关于java - 如何在解析 html 页面时从 html 页面中的 javascript 函数中提取变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11637911/

相关文章:

java - 无法调用匿名类方法

java - 当尝试在静态方法调用中模拟静态方法调用时,Powermock 会调用初始方法

javascript - 我如何让它在 IE6 中呈现?

html - 如何在 CSS 中禁用 div 的倒数第二个子元素的边框?

python - lxml.html 解析和带有请求的 utf-8

java - 通过添加camel功能扩展现有的spring应用程序

java - 是否可以围绕类级别变量创建 AspectJ 切入点?

javascript - 从客户端 Controller (MEAN.JS) 指定 Mongo 查询参数

javascript - 将创建 div 更改为循环,每个都有唯一的 id

javascript - 带边距的 scrollIntoView