English 中文(简体)
利用HtmlUnit搜索角
原标题:Use HtmlUnit to search google
  • 时间:2011-10-24 20:56:49
  •  标签:
  • htmlunit

The following code is an attempt to search google, and return the results as text or html. The code was almost entirely copied directly from code snippets online, and i see no reason for it to not return results from the search. How do you return google search results, using htmlunit to submit the search query, without a browser?

      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();
      }
    } 
最佳回答

某些浏览器必须检测出,改变产生的超文本,因为当用<代码>1.getWebResponse(>)对超文本进行检测时,提交年份的名称是btnG,而不是btnK。 (这不是我所看到的)。 作出这一改变,其结果将是预期的。

问题回答

我只检查了这一情况。 实际为2只角线虫:

  • btnK: on the google home page (where there s 1 long textbox in the middle of the screen). This time the button s id = gbqfa
  • btnG: on the google result page (where the main textbox is on top of the screen). This time the button s id = gbqfb




相关问题
Can t click on a youtube AJAX link with HtmlUnit

I m trying to use htmlunit to click on a link in youtube. Here s the code: HtmlPage page = webClient.getPage("http://www.youtube.com"); HtmlAnchor link = page.getFirstByXPath("//a[contains(@href, ...

How to execute random Javascript code on a web page?

I m using htmlunit to test some pages and I d like to know how can I execute some javascript code in the context of the current page. I m aware that the docs say I d better emulate the behavior of a ...

clicking on submit button using php

I have a webpage with a submit button and I would like php to parse the webpage and click the submit button and get the response (it can be a link or another html page.) Is there any way to click a ...

Does this xpath look correct?

page.getByXPath("//*[@href= http://www.example.com/index.do/abc/1_* "); Do I need to escape any characters? I am trying to get all ahref links on the page that have the pattern of: http://www....

How to navigate to another page after calling click?

I am trying to get all the main links, then click on them and navigation to the page: WebClient client = new WebClient(); HtmlPage page = client.getPage(url); // Get all links with a ...

How to delete cookies?

I want to delete cookies programmatically. More specifically, I am using HtmlUnit with Java to automate browser operations. After performing some operations I want to clear cookies so that my ...

热门标签