2016-07-15 79 views
1

我想從一個文件(a.txt)中搜索我的一些查詢,並在Yahoo!中搜索它們。答案網站,最後寫在另一個文件(b.txt)檢索結果閱讀Yahoo!時出錯。回答網站

我的代碼如下:

public static void run() throws IOException { 
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("XX.XX.XX.XX", 8080)); 
    LineNumberReader lnr = new LineNumberReader(new FileReader(new File("a.txt"))); 
    lnr.skip(Long.MAX_VALUE); 
    int len = lnr.getLineNumber(); 
    lnr.close(); 
    for (int i = 0; i < len; i = i++) { 
     String ll = Files.readAllLines(Paths.get("a.txt")).get(i); 
     String l = URLEncoder.encode(ll, "UTF-8"); 
     String surl = "https://answers.yahoo.com/search/search_result?p=" + l + "&sort=rel"; 
     System.out.println("Search URL: " + surl); 
     URL url = new URL(surl); 
     InputStream in = url.openConnection(proxy).getInputStream(); 
     BufferedReader rd = new BufferedReader(new InputStreamReader(in)); 
     StringBuffer sb = new StringBuffer(); 
     String line; 
     while ((line = rd.readLine()) != null) { 
      PrintWriter pw = new PrintWriter(new FileOutputStream(new File("b.txt"), true)); 
      pw.println(line); 
      pw.close(); 
     } 
     rd.close(); 
    } 

但是,我得到的錯誤如下:

Exception in thread "main" java.io.FileNotFoundException: https://answers.search.yahoo.com/search?p=How+a+13+year+old+boy+can+lose+weight%3F&sort=rel 
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1834) 
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439) 
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254) 
at Yahoo.run(Yahoo.java:117) 
at Main.main(Main.java:36) 

但是,當我在瀏覽器網址中使用搜索字符串時,希望的結果顯示在Yahoo!現場。

回答

2

錯誤不在代碼中。閱讀此問題: Searching in yahoo using java

您將不得不使用BOSS API從現在開始進行搜索。看到這個example並從那裏開始。您必須更改正在進行連接並從yahoo獲取的代碼。一切順利。

+0

它不適用於Yahoo!搜索?使用此代碼,我現在可以訪問其他網站。對於使用BOSS API,根據您的[示例](https://developer.yahoo.com/boss/search/boss_api_guide/codeexamples.html#oauth_java),有哪些必需的jar文件? –