jsoup解析框架集和id属性的问题

时间:2014-06-02 10:54:42

标签: java jsoup frameset

我有这样的源代码:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>SCHOOL-100</title> 
    </head>  
    <frameset rows="111, 1*" border=0>    
        <frame name=top src="top.cgi" marginwidth=0 marginheight=0 noresize scrolling="no"> 
        <frameset cols="200, 1*">     
            <frame name=left namo_target_frame=right src="left.cgi" scrolling=yes>     
            <frame name=right namo_target_frame=_self src="LTE_info.cgi">   
        </frameset>         
        <noframes>         
            <body bgcolor=white text=black link=blue vlink=purple alink=red>  
                <input type=hidden id=age value="12" >  
                <input type=hidden id=class value="9" > 
                <p> </p>    
            </body>      
        </noframes>     
    </frameset>         
</html> 

我正在从网址中获取数据。我试过了:

Document doc = Jsoup.connect("mobile.testmifi/cgi-bin/frame_main.cgi").get();
Elements media = doc.select("noframes");
for (Element src : media) {
    //System.out.println("media source is ---- " + src.text());
}

我面临的问题是我无法在noframe节点之后到达。我想获取id=ageid=class的值。所有这些值都是字符串而不是节点。如果我getElementsbyAttribute("id")它显示为空。

我需要使用jsoup获取年龄/班级(ID)的值,请帮助任何人,提前致谢。

1 个答案:

答案 0 :(得分:2)

这里的问题是noframes不被识别为标准标签,而html inside将被视为其值。如果您只想获取年龄和类的值,您可以获取noframes标记的值并将其解析为正文片段然后阅读。例如,

import java.io.File;
import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;


public class JsoupParser4 {

    public static void main(String[] args) {
        try {
            Document doc = Jsoup.parse(new File("mob.html"), "UTF-8");
            Document noFramesDoc = Jsoup.parseBodyFragment(doc.select("noframes").text());
            System.out.println("Age = " + noFramesDoc.select("input[id=age]").attr("value"));
            System.out.println("Class = " + noFramesDoc.select("input[id=class]").attr("value"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

mob.html文件包含问题中的html标记代码。