javascript 如何使用jsoup提交表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13775820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:44:42  来源:igfitidea点击:

how to submit form using jsoup

javajavascriptscreen-scrapingjsoup

提问by user1701556

I want to submit form from a webpage which has several forms. I want to submit this form below in particular.

我想从包含多个表单的网页提交表单。我特别想在下面提交此表格。

<form action="realDisplay.asp" method="post" name="Search" onSubmit="return validate(this); return submitForm();" target="_blank">
               <table width="98%" align="center" cellspacing="0" cellpadding="0" border="1" bordercolor="#FFFFFF">
              <tr>
                 <td width="127" class="style62" align="right">Parcel ID</td>
                <td width="286">
                  <div align="center">
                    <table border="0" cellpadding="0" cellspacing="0" width="100%%" align="center">
                      <tr>
                        <td align="left" class="style62">&nbsp;
                        <input name="rePID" size="15" maxlength="15" value="">
                         <br>
                          <font class="style65">( 12 123 12 123)</font></td>
                      </tr>
                    </table>
                  </div> 
                </td>
                </tr>
                <tr>
                 <td width="127" class="style62" align="right">Partial Parcel ID</td>
                <td width="286">
                  <div align="center">
                    <table border="0" cellpadding="0" cellspacing="0" width="100%%" align="center">
                      <tr>
                        <td align="left" class="style62">&nbsp;
                        <input name="rePartialPID" size="15" maxlength="15" value="">
                         <br>
                          <font class="style65">( 12 123)</font></td>
                      </tr>
                    </table>
                  </div> 
                </td>
                </tr>
              <tr>
                <td class="style62" align="right">Address</td>
                <td class="style62" align="left">&nbsp;
                  <input name="Address" size="38" maxlength="50" value="">
                </td>
              </tr>
               <tr>
                <td class="style62" align="right" nowrap="nowrap">Partial Street Name</td>
                <td class="style62" align="left">&nbsp;
                  <input name="streetName" size="38" maxlength="50" value="">
                </td>
              </tr>
              <tr>
                <td class="style62" align="right" nowrap="nowrap">Owner Name</td>
                <td class="style62" align="left">&nbsp;
                  <input name="OwnerName" size="30" maxlength="50" value="">   
                </td>
              </tr>
              <tr>
                <td colspan="2"><br /><font class="style64">Insert</font>
                  <font class="style65"><u>Either</u></b></font><font class="style64">&nbsp;a</font>:<br>
                 <br>
                 <table align="center" border="1" cellspacing="0" cellpadding="3" bgcolor="#E8E8E8">
                    <tr>
                      <td align="left"><img src="Images/arrow_1.gif" width="9" height="7" vspace="0" hspace="8"></td>
                      <td class="style75" align="left">
                      <a class="nav4" href="GlossaryTermWin.htm#ParcelID" onClick="NewWindow(this.href,'PARCELID','635','635','yes');return false;">
                     Parcel ID</a> , or Partial Parcel ID</td>
                    </tr>
                    <tr>
                    </tr>
                    <tr>
                      <td>
                      <img src="Images/arrow_1.gif" width="9" height="7" vspace="0" hspace="8"></td>
                      <td class="style75" align="left">Address (eg. 123 main), or</td>
                    </tr>
                    <tr>
                      <td class="style75" align="left">
                      <img src="Images/arrow_1.gif" width="9" height="7" vspace="0" hspace="8"></b></font></td>
                      <td class="style75">Partial Street Name (eg. main), or</td>
                    </tr>
                    <tr>
                      <td class="style75">
                      <img src="Images/arrow_1.gif" width="9" height="7" vspace="0" hspace="8"></b></font></td>
                      <td class="style75" align="left">Owner Name <br />(eg. LastName,FirstName <br />or
                              Partial Owner Name)</td>
                    </tr>
                </table> </td>
              </tr>
              <tr>
                 <td colspan="2">&nbsp;</td>
                    </tr>
              <tr>
                <td height="26" colspan="2" align="center">
                <input type="image" valign="top" name="Submit" value="Search" src="images/search.jpg" align="top" alt="Search by either the Parcel ID or Address or Owner Name that is associated by the real estate information.">
                <a href="javascript:document.forms[0].reset()" border="0"><img src="images/reset.jpg" align="top" border="0" alt="Reset the values on this page." onClick="ResetForm()"></a></td>
              </tr>
  </table>
</form></td>

My Java Code looks like this:

我的 Java 代码如下所示:

Document doc = Jsoup.connect("http://web.somewebsite.asp")
                   .data("rePID", "15 197 14 007")
                   .post();

I want to able to view the webpage that comes up after I submit this form, and also view the text content of that page, and also want to get url as well.

我希望能够查看提交此表单后出现的网页,并查看该页面的文本内容,还希望获取 url。

What would I have to do in my Java code to make sure that submit request was submitted and view the content of next page after submitting the form.

我需要在我的 Java 代码中做什么来确保提交请求已提交并在提交表单后查看下一页的内容。

I was able to submit form using Htmlunit, you can search the web and download the package.

我可以使用 Htmlunit 提交表单,您可以在网上搜索并下载包。

import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlImageInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;

WebClient webClient = new WebClient(BrowserVersion.FIREFOX_10);
final HtmlPage searchPage = webClient.getPage("http://web.somewebsite.asp"); 

    final HtmlForm form = searchPage.getFormByName("Search"); 
    final HtmlTextInput textField = form.getInputByName("rePID");
    textField.setValueAttribute("15 197 14 007");
    final HtmlImageInput button = form.getInputByName("Submit"); 
    HtmlPage searchResultPage = (HtmlPage)button.click();

回答by Wojciech Jakubas

First of all, you will need to investigate the page itself. Chrome has nice build in feature to support it. Just press F12 and you will see quite few options and you can investigate almost everything related to particular HTML request. Newer versions of IE also have something similar. Firefox has great firebug extension.

首先,您需要调查页面本身。Chrome 有很好的内置功能来支持它。只需按 F12,您将看到很少的选项,您可以调查与特定 HTML 请求相关的几乎所有内容。较新版本的 IE 也有类似的东西。Firefox 有很棒的 firebug 扩展。

You can see Cookies that are used by the server. Maybe you are missing them. Quite often most important one will be Set-Cookie. You will need to add it to Cookie Post request you will be sending in POST Html request.

您可以看到服务器使用的 Cookie。也许你正在想念他们。通常最重要的是 Set-Cookie。您需要将它添加到您将在 POST Html 请求中发送的 Cookie Post 请求。

In your case, you will also need to check java Script function SubmitForm. Please remember that JavaScript probably will not be called from your code. Your page you need to send request to will be "realDisplay.asp".

在您的情况下,您还需要检查 java Script 函数 SubmitForm。请记住,您的代码可能不会调用 JavaScript。您需要向其发送请求的页面将是“realDisplay.asp”。

Sometime, browser send post request with some parameters in URL string, like with GET. Please check them too. Make your POST requests exactly the same as used by the browser.

有时,浏览器会发送带有 URL 字符串中的一些参数的 post 请求,例如 GET。请也检查它们。使您的 POST 请求与浏览器使用的完全相同。

Please check how long is the session while logged in via browser. To stay in session you will need to send some requests to the application. If they are not sent you will need to re-log in again.

请检查通过浏览器登录时的会话时间。要保持会话,您需要向应用程序发送一些请求。如果未发送,您将需要重新登录。

There is another answer for using jsoup with cookies.

将 jsoup 与 cookie 一起使用还有另一个答案。

jsoup posting and cookie

jsoup 发布和 cookie

Connection.Response res = Jsoup.connect("http://www.example.com/login.php")
.data("username", "myUsername", "password", "myPassword")
.method(Method.POST)
.execute();

Document doc = res.parse();

If you need more info please let me know.

如果您需要更多信息,请告诉我。