I am scraping a url(http://nvsos.gov/sosentitysearch/CorpSearch.aspx) in jsoup , However I am able to scrape the first page but unable to navigate to second page.
Here is the code snippet
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();
}
Here .data("ctl00$MainContent$objSearchGrid$dgCorpSearchResults$ctl54$ctl01", "")
is to navigate to second page but it always returns first page.
You may be missing some cookies. Try the code below:
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();