次のコードは、Googleを検索し、その結果をテキストまたはHTML形式で返します。 コードはほぼ完全にコードスニペットのオンラインから直接コピーされていましたが、検索結果を返さない理由はありません。どのようにhtmlunitを使用して、ブラウザを使わずに検索クエリを提出すると、Googleの検索結果を返すのですか?HtmlUnitを使用してGoogleを検索する
import com.gargoylesoftware.htmlunit.WebClient;
import java.io.*;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import java.net.*;
public class GoogleSearch {
public static void main(String[] args)throws IOException, MalformedURLException
{
final WebClient webClient = new WebClient();
HtmlPage page1 = webClient.getPage("http://www.google.com");
HtmlInput input1 = page1.getElementByName("q");
input1.setValueAttribute("yarn");
HtmlSubmitInput submit1 = page1.getElementByName("btnK");
page1=submit1.click();
System.out.println(page1.asXml());
webClient.closeAllWindows();
}
}
あなたはおそらく、プログラムが何をしているか教えていただけます。私たちはそれが何をすべきかを知っていますが、それは何をしていません。例外はありますか?スタックトレース? –
実行され、例外なしで正常にビルドされますが、xml/htmlは出力されません。 btnGが動作し、なぜfirefoxがbtnGが必要なbtnKが見えるのかわかりません。それはまた、パーサーとの間でもよい。 –