在报废时无法通过jsoup导航aspx页面

时间:2015-06-15 17:16:00

标签: java c# asp.net web-scraping jsoup

我在jsoup中删除了一个网址(http://nvsos.gov/sosentitysearch/CorpSearch.aspx),但是我可以废弃第一页但无法导航到第二页。

以下是代码段

try
{
    string url = "http://nvsos.gov/sosentitysearch/CorpSearch.aspx";
    Connection.Response response = Jsoup.connect(url).method(Connection.Method.GET).execute();
    Document responseDocument = response.parse();

    Element eventValidation = responseDocument.select("input[name=__EVENTVALIDATION]").first();
    Element viewState = responseDocument.select("input[name=__VIEWSTATE]").first();

    //javascript:__doPostBack('ctl00$MainContent$objSearchGrid$dgCorpSearchResults$ctl54$ctl01','')
    response = Jsoup.connect(url)
    .data("__VIEWSTATE", viewState.attr("value"))
    .data("__EVENTVALIDATION", eventValidation.attr("value"))
    .data("ctl00$MainContent$txtSearchBox", "apple")  // <- search 
    .data("ctl00$MainContent$btnCorpSearch", "Search")
    .data("ctl00$MainContent$ddlCorpSortColumns", "m")
    .data("ctl00$MainContent$ddlCorpNumSortColumns", "m")
    .data("ctl00$MainContent$ddlOfficerSortColumns", "m")
    .data("ctl00$MainContent$ddlRASortColumns", "m")
    .data("ctl00$MainContent$ddlABNSortColumns", "m")
    .data("ctl00$MainContent$ddlABNSortColumns", "m")
    .data("ctl00$MainContent$rdlSortOrder", "d")
    .data("ctl00$MainContent$objSearchGrid$dgCorpSearchResults$ctl54$ctl01", "")
    .method(Connection.Method.POST)
    .followRedirects(true)
    .execute();

    Document document = response.parse(); //search results
    System.out.println(document);

}
catch (IOException e)
{
    e.printStackTrace();
}

此处.data("ctl00$MainContent$objSearchGrid$dgCorpSearchResults$ctl54$ctl01", "")将导航到第二页,但它始终会返回第一页。

1 个答案:

答案 0 :(得分:0)

您可能遗漏了一些Cookie。请尝试以下代码:

response = Jsoup.connect(url)
.cookies(response.cookies()) // Add cookies received when fetching the first page
.data("__VIEWSTATE", viewState.attr("value"))
.data("__EVENTVALIDATION", eventValidation.attr("value"))
.data("ctl00$MainContent$txtSearchBox", "apple")  // <- search 
.data("ctl00$MainContent$btnCorpSearch", "Search")
.data("ctl00$MainContent$ddlCorpSortColumns", "m")
.data("ctl00$MainContent$ddlCorpNumSortColumns", "m")
.data("ctl00$MainContent$ddlOfficerSortColumns", "m")
.data("ctl00$MainContent$ddlRASortColumns", "m")
.data("ctl00$MainContent$ddlABNSortColumns", "m")
.data("ctl00$MainContent$ddlABNSortColumns", "m")
.data("ctl00$MainContent$rdlSortOrder", "d")
.data("ctl00$MainContent$objSearchGrid$dgCorpSearchResults$ctl54$ctl01", "")
.method(Connection.Method.POST)
.followRedirects(true)
.execute();