Exemplo n.º 1
0
  public void getGoogleUrls(String inFilename, String outFilename) throws IOException {
    GoogleSearcher gsearch = new GoogleSearcher(50);
    File wf = new File(outFilename);
    FileWriter fileWriter = null;
    PrintWriter writer = null;
    if (wf.exists()) { // 文件如果存在,则以追加的形式写入
      fileWriter = new FileWriter(wf, true);

    } else {
      wf.createNewFile();
      fileWriter = new FileWriter(wf);
    }
    writer = new PrintWriter(fileWriter);
    BufferedReader bReader = new BufferedReader(new FileReader(new File(inFilename)));
    String keyWord;
    while ((keyWord = bReader.readLine()) != null) {
      gsearch.googlesearch(keyWord);
      // 把结果写入文件
      int num = gsearch.getAmountOfResults();
      GoogleSearchResult[] gresults = gsearch.getSearchResults();
      for (int i = 0; i < num; i++) {
        writer.println(gresults[i].getUrl());
        writer.flush();
      }
    }
    if (fileWriter != null) fileWriter.close();
    if (writer != null) writer.close();
    if (bReader != null) bReader.close();
  }
Exemplo n.º 2
0
 /**
  * 输入特定的关键词,需要解析的记录条数。程序返回解析得到URL列表。
  *
  * @param keyword:需要输入的关键词
  * @param recordNumber:需要解析的记录条数.
  * @return :返回所有结果的URL。
  */
 public List<String> searchUrl(String keyword, int recordNumber) {
   ArrayList<String> urlList = new ArrayList<String>();
   GoogleSearcher gsearch = new GoogleSearcher(recordNumber);
   try {
     gsearch.googlesearch(keyword);
     GoogleSearchResult[] gresults = gsearch.getSearchResults();
     int num = gsearch.getAmountOfResults();
     if (num > 0) {
       for (int i = 0; i < num; i++) {
         urlList.add(gresults[i].getUrl());
       }
     }
   } catch (UnsupportedEncodingException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
   return urlList;
 }