鍍金池/ 教程/ Java/ Jsoup示例:提取表單參數(shù)
JSoup安裝
JSoup教程
Jsoup應(yīng)用實(shí)例
Jsoup API
Jsoup示例:提取給定url的標(biāo)題
JSoup快速入門
Jsoup示例:提取給定URL中的鏈接
Jsoup示例:提取URL中的元數(shù)據(jù)
Jsoup示例:提取URL中的圖像
Jsoup示例:提取表單參數(shù)

Jsoup示例:提取表單參數(shù)

在這個(gè)例子中,我們將提取并打印表單參數(shù),如參數(shù)名稱和參數(shù)值。 為此,我們調(diào)用Document類的getElementById()方法和Element類的getElementsByTag()方法。

創(chuàng)建一個(gè)HTML文件:register.html,代碼如下 -

<!DOCTYPE html>  
<html>  
<head>  
<meta charset="utf-8">  
<title>Register Please</title>  
</head>  
<body>  
<form id="registerform" action="register.jsp" method="post">  
Name:<input type="text" name="name" value="maxsu"/><br/>  
Password:<input type="password" name="password" value="sj"/><br/>  
Email:<input type="email" name="email" value="maxsu@gmail.com"/><br/>  
<input name="submitbutton" type="submit" value="register"/>  
</form>  
</body>  
</html>  
`

創(chuàng)建一個(gè)Java類文件:JsoupPrintFormParameters.java,代碼如下 -

import java.io.File;  
import java.io.IOException;  
import org.jsoup.Jsoup;  
import org.jsoup.nodes.Document;  
import org.jsoup.nodes.Element;  
import org.jsoup.select.Elements;  
public class JsoupPrintFormParameters {  
public static void main(String[] args) throws IOException {  
    Document doc = Jsoup.parse(new File("e:\\register.html"),"utf-8");  
    Element loginform = doc.getElementById("registerform");  

    Elements inputElements = loginform.getElementsByTag("input");  
    for (Element inputElement : inputElements) {  
        String key = inputElement.attr("name");  
        String value = inputElement.attr("value");  
        System.out.println("Param name: "+key+" \nParam value: "+value);  
    }  
}  
}

執(zhí)行結(jié)果 -

Param name: name 
Param value: maxsu
Param name: password 
Param value: sj
Param name: email 
Param value: maxsu@gmail.com
Param name: submitbutton 
Param value: register

自已編程運(yùn)行看看吧


上一篇:JSoup快速入門下一篇:JSoup教程