如何使用Jsoup填写表单? [英] How to fill a form with Jsoup?

查看:148
本文介绍了如何使用Jsoup填写表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试浏览加州网站的描述页面 http://kepler.sos.ca.gov/ 。但是无法前往。

I am trying to navigate to description page of California website http://kepler.sos.ca.gov/. but unable to go .

然后,我有一个html表单,我提交请求,
我无法在这里添加表单但是它很简单 POST 请求 http://kepler.sos.ca。带有所需参数的gov /

Then,I have a html form, on which I am submitting request, I am unable to add form here but its simple a POST request to http://kepler.sos.ca.gov/ with required params

我能够获得 __ EVENTTARGET __EVENTARGUMENT 来自我来到这里的上一页。

I am able to get __EVENTTARGET and __EVENTARGUMENT from previous page from which I came here.

我做错了什么?

代码:

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();


推荐答案

你想使用 FormElement 。这是Jsoup的一个有用功能。它能够找到在表单中声明的​​字段并为您发布它们。在发布表单之前,您可以使用Jsoup API设置字段的值。

You want to use FormElement. This is a useful feature of Jsoup. It is able to find the fields declared inside a form and post them for you. Before posting the form you can set the value of the fields using Jsoup API.


Nota:

在下面的示例代码中,您将始终看到对元素#select 方法,然后调用元素#first 方法。

In the sample codes below, you'll always see calls to the Element#select method followed by a call to Elements#first method.

例如:
responseDocument.select(form#aspnetForm)。first()

Jsoup 1.11.1 引入了一种更有效的替代方案:元素#selectFirst 。您可以将其用作原始替代品的直接替代品。

Jsoup 1.11.1 has introduced a more efficient alternative : Element#selectFirst. You can use it as a direct replacement of the original alternative.

例如:

responseDocument.select(表格#aspnetForm)。first()

可以替换为
responseDocument.selectFirst(form#aspnetForm)



SAMPLE CODE



SAMPLE CODE

// * 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);
    }
}



输出(撰写本文时)



OUTPUT (as of this writing)

C3036475
C3027305
C3236514
C3027304
C3034012
C3035110
C3028330
C3035378
C3124793
C3734637



参见:



在本例中,我们将登录 GitHub 网站:使用 FormElement 课程。

// # 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类处理我们(甚至形式方法检测)。在调用连接: //jsoup.org/apidocs/org/jsoup/nodes/FormElement.html#submit--rel =nofollow noreferrer> FormElement#submit 方法。我们所要做的就是用附加标题(cookies,用户代理等)完成这个连接并执行它。

All the form data is handled by the FormElement class for us (even the form method detection). A ready made Connection is built when invoking the FormElement#submit method. All we have to do is to complete this connection with addional headers (cookies, user-agent etc) and execute it.

这篇关于如何使用Jsoup填写表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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