阅读 Yahoo! 时出错问答网站

Error while reading Yahoo! Answers website

我想从文件 (a.txt) 中搜索我的一些查询并在 Yahoo! 中搜索它们答题网站,最后将检索结果写入另一个文件(b.txt)

我的代码如下:

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!网站。

错误不在代码中。阅读这个问题: Searching in yahoo using java

您将不得不使用 BOSS API to do your searching from now on. See this example 并从那里开始。您必须更改执行连接并从 yahoo.All 中获取数据的代码。