Javaでプログラム的にWebページにアクセスする方法 質問する

Javaでプログラム的にWebページにアクセスする方法 質問する

特定の文字列を取得したい Web ページがあります。そのためには、ログインし、いくつかのボタンをクリックし、テキスト ボックスに入力し、別のボタンをクリックする必要があります。すると、文字列が表示されます。

これを自動的に実行する Java プログラムを作成するにはどうすればよいでしょうか? そのために役立つライブラリはありますか?

ありがとう

ベストアンサー1

試すHTMLユニット

HtmlUnit は、「Java プログラム用の GUI なしのブラウザ」です。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

おすすめ記事