2016-07-15 6 views
1

ファイル(a.txt)からいくつかのクエリを検索し、Yahoo!で検索したい回答のサイトと、最終的には別のファイル(B.TXT)で取得した結果を書き込むYahoo! Answers website

次のように私のコードは次のとおりです。

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) 

しかし、私はブラウザのURLで検索文字列を使用している場合、希望の結果は、Yahoo!サイト。

答えて

2

エラーはコードに含まれていません。この質問を読む: Searching in yahoo using java

あなたは今からあなたの検索を行うためにBOSS APIを使用する必要があります。このexampleを参照して、そこから始めてください。 yahoo.Allから最高の接続とフェッチを行っているコードを変更する必要があります。

+0

Yahoo!サーチ?このコードを使用すると、私はまだ他のサイトにアクセスできます。 BOSS APIを使用するには、[例](https://developer.yahoo.com/boss/search/boss_api_guide/codeexamples.html#oauth_java)に必要なjarファイルは何ですか? –