【问题标题】:Error while reading Yahoo! Answers website阅读 Yahoo! 时出错答案网站
【发布时间】:2016-11-18 21:31:03
【问题描述】:

我想从文件 (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!网站。

【问题讨论】:

    标签: java web-scraping yahoo


    【解决方案1】:

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

    从现在开始,您将不得不使用BOSS API 进行搜索。看到这个example 并从那里开始。您必须更改正在执行连接并从 yahoo 获取的代码。一切顺利。

    【讨论】:

    • 它不仅适用于 Yahoo!搜索?使用此代码,我现在仍然可以访问其他站点。对于使用 BOSS API,根据您的example,需要哪些 jar 文件?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    • 2016-11-05
    • 2021-08-07
    • 2011-07-29
    相关资源
    最近更新 更多