如何使用jsoup提交表单

时间:2012-12-08 08:30:01

标签: java javascript screen-scraping jsoup

我想从具有多种表单的网页提交表单。我想特别提交以下表格。

<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>

我的Java代码如下所示:

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

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

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

我可以使用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();

1 个答案:

答案 0 :(得分:3)

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

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

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

有时,浏览器会在URL字符串中发送带有一些参数的发布请求,就像使用GET一样。 请检查一下。使您的POST请求与浏览器使用的完全相同。

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

使用jsoup和cookies还有另一个答案。

jsoup posting and 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();

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