程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何用Jsoup填写表格?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何用Jsoup填写表格??

开发过程中遇到如何用Jsoup填写表格?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何用Jsoup填写表格?的解决方法建议,希望对你解决如何用Jsoup填写表格?有所启发或帮助;

您要使用FormElement。这是Jsoup的有用功能。它能够找到在表单内声明的字段并将其发布给您。发布表单之前,您可以使用Jsoup API设置字段的值

样本代码

// * Connect to website
String url = "http://kepler.sos.ca.gov/";
Connection.Response resp = Jsoup.connect(url) //
                                .timeout(30000) //
                                .method(Connection.Method.GET) //
                                .execute();

// * Find the form
document responsedocument = resp.parse();
Element potentialForm = responsedocument.SELEct("form#aspnetForm").first();
checkElement("form element", potentialForm);
FormElement form = (FormElement) potentialForm;

// * Fill in the form and submit it
// ** Search Type
Element radiobuttonListSearchType = form.SELEct("[name$=RadiobuttonList_SearchType]").first();
checkElement("search type radio button List", radiobuttonListSearchTypE);
radiobuttonListSearchType.attr("checked", "checked");

// ** name search
Element textBoxnameSearch = form.SELEct("[name$=TextBox_nameSearch]").first();
checkElement("name search text Box", textBoxnameSearch);
textBoxnameSearch.val("cali");

// ** submit the form
document searchResults = form.submit().cookies(resp.cookies()).post();

// * Extract results (entity numbers in this sample codE)
for (Element entitynumber : searchResults.SELEct("table[ID$=SearchResults_Corp] > tbody > tr > td:first-of-type:not(td[colspan=5])")) {
    System.out.println(entitynumber.text());
}

public static voID checkElement(String name, Element elem) {
    if (elem == null) {
        throw new RuntimeException("Unable to find " + Name);
    }
}

输出(截至撰写本文时)

C3036475
C3027305
C3236514
C3027304
C3034012
C3035110
C3028330
C3035378
C3124793
C3734637

也可以看看:

在此示例中,我们将使用FormElement类登录GitHub网站。

// # Constants used in this example
final String user_ageNT = "Mozilla/5.0 (windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"; 
final String LOGIN_FORM_URL = "https://github.com/login";
final String USERname = "yourUsername";  
final String passworD = "yourpassword";

// # Go to login page
Connection.Response loginFormResponse = Jsoup.connect(LOGIN_FORM_URL)
                                             .method(Connection.Method.GET)
                                             .userAgent(user_ageNT)
                                             .execute();

// # Fill the login form
// ## Find the form first...
FormElement loginForm = (FormElement)loginFormResponse.parse()
                                         .SELEct("div#login > form").first();
checkElement("Login Form", loginForm);

// ## ... then "type" the username ...
Element loginFIEld = loginForm.SELEct("#login_fIEld").first();
checkElement("Login FIEld", loginFIEld);
loginFIEld.val(USERName);

// ## ... and "type" the password
Element passwordFIEld = loginForm.SELEct("#password").first();
checkElement("password FIEld", passwordFIEld);
passwordFIEld.val(passworD);


// # Now send the form for login
Connection.Response loginActionResponse = loginForm.submit()
         .cookies(loginFormResponse.cookies())
         .userAgent(user_ageNT)  
         .execute();

System.out.println(loginActionResponse.parse().HTML());

public static voID checkElement(String name, Element elem) {
    if (elem == null) {
        throw new RuntimeException("Unable to find " + Name);
    }
}

所有表单数据都由FormElement类为我们处理(甚至包括表单方法检测)。调用FormElement#submit方法时,将建立一个现成的Connection。我们要做的就是用附加头(cookie,用户代理等)完成此连接并执行它。

解决方法

我试图浏览到加利福尼亚网站http://kepler.sos.ca.gov/的描述页面。但无法走。

然后,我有一个html表单,在该表单上我正在提交请求,我无法在此处添加表单,但是它只是带有必需参数的POSThttp://kepler.sos.ca.gov/的请求

我能得到__EVENTTARGET并且__EVENTARGUMENT从我来到这里之前的页面。

我究竟做错了什么?

码:

String url = "kepler.sos.ca.gov/";
Connection.Response resp = Jsoup.connect(url)
                                .timeout(30000)
                                .method(Connection.Method.GET) 
                                .execute();
Document responseDocument = resp.parse();
Map<String,String> loginCookies = resp.cookies();
   eventValidation=responseDocument.SELEct("input[name=__EVENTVALIDATION]").first();
viewState = responseDocument.SELEct("input[name=__VIEWSTATE]").first();

大佬总结

以上是大佬教程为你收集整理的如何用Jsoup填写表格?全部内容,希望文章能够帮你解决如何用Jsoup填写表格?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: