简体   繁体   中英

Get a part of a webpage using JSOUP

I am trying to programmatically search for a word meaning in google & save its meaning in a file in my computer. I have successfully called the page & get the response in Document (org.jsoup.nodes.Document). Now I do not know how to get only the word meaning from this Document. Please find the screenshot where I have indicated the part of data that I need. 在此处输入图片说明

The response html is so big that I can't understand from which element I will get my desired data. Please help. Here is what I have done so far:

public class Search {
     private static Pattern patternDomainName;
  private Matcher matcher;
  private static final String DOMAIN_NAME_PATTERN 
    = "([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}";

  static {
    patternDomainName = Pattern.compile(DOMAIN_NAME_PATTERN);
  }





  public static void main(String[] args) {

      Search obj = new Search();
    Set<String> result = obj.getDataFromGoogle("debug%20meaning");
    for(String temp : result){
        System.out.println(temp);
    }
    System.out.println(result.size());
  }





  public String getDomainName(String url){

    String domainName = "";
    matcher = patternDomainName.matcher(url);
    if (matcher.find()) {
        domainName = matcher.group(0).toLowerCase().trim();
    }
    return domainName;

  }




  private Set<String> getDataFromGoogle(String query) {

    Set<String> result = new HashSet<String>(); 
    String request = "https://www.google.com/search?q=" + query + "&num=20";
    System.out.println("Sending request..." + request);

    try {

        // need http protocol, set this as a Google bot agent :)
        Document doc = Jsoup
            .connect(request)
            .userAgent(
              "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)")
            .timeout(5000).get();


        /**********Here comes my data fetching logic*****************
         * Dont know where to fing my desired data in such a big html
         */

    /*
        String sc = doc.html().replaceAll("\\n", "");
        System.out.println(doc.html());
        */

    } catch (IOException e) {
        e.printStackTrace();
    }

    return result;
  }

}

But instead scraping through google search URI,which is what you are doing currently, you can do the same thing using this http://google-dictionary.so8848.com/ service which preferably more easy to scrape data from, with what you are doing currently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM