我想要从一个网页中检索特定的字符串。为此,我需要登录,点击一些按钮,填写文本框,再点击另一个按钮-然后字符串就出现了。
我该如何编写Java程序以自动完成这些操作?是否有相关的有用库可供使用?
谢谢
尝试使用HtmlUnit
HtmlUnit是一个“无界面浏览器”,适用于Java程序。它模拟HTML文档并提供API,允许您调用页面、填写表单、点击链接等操作,就像在您的“普通”浏览器中一样。
提交表单的示例代码:
@Test
public void submittingForm() throws Exception {
final WebClient webClient = new WebClient();
// Get the first page
final HtmlPage page1 = webClient.getPage("http://some_url");
// 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.getFormByName("myform");
final HtmlSubmitInput button = form.getInputByName("submitbutton");
final HtmlTextInput textField = form.getInputByName("userid");
// Change the value of the text field
textField.setValueAttribute("root");
// Now submit the form by clicking the button and get back the second page.
final HtmlPage page2 = button.click();
webClient.closeAllWindows();
}
更多详情请查看: http://htmlunit.sourceforge.net/gettingStarted.html
@Test
public void homePage() throws Exception {
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText());
}
是的:
java.net.URL#openConnection()
可以让您发起 HTTP 请求并获取 HTTP 响应
Apache HttpComponents 是一个使得使用 HTTP 更加容易的库。