Automatic interactions with web pages
I need to retrive information from a web page which accept only a form as an input.
I usually use HttpClient to retrieve info and parse the html, but this page take a Javascript rather than an explicit url as argument, and print input again as a javascript.
I tried HTMLUnit but is painfully slow, especially on Android. Moreover it seems like I can't make it works correctly, as I'm not able to retrive the html after filling the form, maybe due to the page being a javascrpit (see attached code).
How can I retrive information from this web page in Android?
Cheers :D
package com.htmlUnitTest;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
public class HtmlUnitTest {
/**
* @param args
*/
public static void main(String[] args) {
try {
submittingForm();
} catch (Exception e) {
System.out.println("Something bad happened");
e.printStackTrace();
}
}
static public void submittingForm() throws Exception {
final WebClient webClient = new WebClient();
// Get the first page
final HtmlPage page1 = webClient.getPage(
"http://mobile.viaggiatreno.it/viaggiatreno/mobile/stazione?lang=IT");开发者_运维技巧
System.out.println(page1.getForms());
// Get the form that we are dealing with and within that form,
// find the submit button and the field that we want to change.
final HtmlForm form = page1.getForms().get(0);
final HtmlSubmitInput button = form.getInputByValue(" Cerca ");
final HtmlTextInput textField = form.getInputByName("stazione");
// Change the value of the text field
textField.setValueAttribute("Milano Centrale");
// Now submit the form by clicking the button and get back the second page.
final HtmlPage page2 = button.click();
System.out.println(page2.getAnchors());
webClient.closeAllWindows();
}
}
You need to POST to /viaggiatreno/mobile/stazione, parse the answer, then post again if you want to drill down. More info about posting here: google.com/search?q=webClient+post
精彩评论