简体   繁体   English

Yahoo Finance API Java下载CSV

[英]Yahoo Finance API Java Download CSV

I have been using the Yahoo Finance "API" with the code below for a few weeks. 我已经在下面的代码中使用Yahoo Finance“ API”了几周了。 Since about a week ago, it stopped working. 从大约一周前开始,它停止工作。 If you type in this address: http://ichart.yahoo.com/table.csv?s=MSFT , you'll get some historical data for Microsoft (my browser automatically downloads it). 如果您输入以下地址: http : //ichart.yahoo.com/table.csv?s=MSFT ,您将获得一些Microsoft的历史数据(我的浏览器会自动下载它)。 However, when I try to read it through Java, the stream is apparently opened (I don't get an exception), however, the stream contains no data. 但是,当我尝试通过Java读取它时,该流显然已打开(我没有得到异常),但是,该流不包含任何数据。 Anyone know why the buffered reader below is not able to stream the object as it had been able to do in previous weeks? 有人知道为什么下面的缓冲阅读器无法像以前几周那样能够流传输对象吗? I suspect that maybe Yahoo added some java script to block automated downloading. 我怀疑雅虎也许添加了一些Java脚本来阻止自动下载。

URL url = new URL("http://ichart.yahoo.com/table.csv?s=" + symbol);
URLConnection hc = url.openConnection();
hc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 4.10; rv:52.0) Gecko/20100101 Firefox/52.0");
BufferedReader in = new BufferedReader(new InputStreamReader(hc.getInputStream()));
    br.readLine();
    //Read File Line By Line
    String strLine;
    while ((strLine = br.readLine()) != null) {
        System.out.println(strLine);
    }
    br.close();

Many Websites are checking for a User Agent String and block acess if no String has been sent to block automated access. 许多网站正在检查用户代理字符串,如果没有发送任何字符串来阻止自动访问,则会阻止访问。

This is an example how you can add the user agent string: 这是一个如何添加用户代理字符串的示例:

    URL url = new URL("http://ichart.yahoo.com/table.csv?s=MSFT");
    URLConnection hc = url.openConnection();
    hc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 4.10; rv:52.0) Gecko/20100101 Firefox/52.0");

To understand the reason the java communication doesn't work, track the html communication. 要了解Java通信无法正常工作的原因,请跟踪html通信。 I used Chrome browser, with HTTP trace extension enabled. 我使用了启用了HTTP跟踪扩展名的Chrome浏览器。 The trace output shows that the link http://ichart.yahoo.com/table.csv?s= ^GSPC redirects to https://ichart.yahoo.com/table.csv?s= ^GSPC. 跟踪输出显示链接http://ichart.yahoo.com/table.csv?s= ^ GSPC重定向到https://ichart.yahoo.com/table.csv?s= ^ GSPC。

In Java, you need to implement a URL connection to follow the redirect. 在Java中,您需要实现URL连接以遵循重定向。 An example of following the redirect is at http://www.mkyong.com/java/java-httpurlconnection-follow-redirect-example/ . 遵循重定向的示例位于http://www.mkyong.com/java/java-httpurlconnection-follow-redirect-example/ Modify the example by passing your url string, and you should get the expected output. 通过传递您的url字符串修改示例,您应该获得预期的输出。

您可以将URL从“ http”更改为“ https”,这样可以正常工作。

Try executing this code from some other network. 尝试从其他网络执行此代码。 That'll give you clarity whether Yahoo has blocked automated downloading or only blacklisted the network you're using. 这样一来,您就可以清楚地了解Yahoo是阻止自动下载还是仅将您正在使用的网络列入了黑名单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM