扫描程序(Getinputstream())抛出异常返回没有从URL连接找到的行

时间:2017-04-23 20:08:02

标签: java url web-scraping java.util.scanner yahoo-finance

我正在尝试编写一个Java程序,它建立与雅虎财务的连接,并为特定股票提取网站的一些数据。

程序终止,但没有找到行,这是在if(input.hasNextLine())语句中抛出的。我得到了异常的意思,但我无法弄清楚错误是什么。

我知道问题不在URL构造中,因为URL在复制到Web浏览器时会从Web下载所请求的数据。

希望有人可以指出我正确的方向,我已经困惑了几个小时,试图搜索论坛,但到目前为止没有运气。

我的代码如下:

import java.net.URL;
import java.net.URLConnection;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class Download {
    public Download(String symbol, GregorianCalendar end, GregorianCalendar start){

        //Creates the URL
        String url = "http://chart.finance.yahoo.com/table.csv?s="+symbol+
                     "&a="+start.get(Calendar.MONTH)+
                     "&b="+start.get(Calendar.DAY_OF_MONTH)+
                     "&c="+start.get(Calendar.YEAR)+
                     "&d="+end.get(Calendar.MONTH)+
                     "&e="+end.get(Calendar.DAY_OF_MONTH)+
                     "&f="+end.get(Calendar.YEAR)+
                     "&g=d&ignore=.csv";

        try{
        //Creates the URL object, and establishes connection    
        URL yhoofin = new URL(url); 
        URLConnection data = yhoofin.openConnection();
        //Opens an input stream, to read from
        Scanner input = new Scanner(data.getInputStream(),"UTF-8");

        System.out.println(input.nextLine());
        //skips the first line, 
            if(input.hasNextLine()){
                input.nextLine();
                //tries to print the data.
                    while(input.hasNextLine()){
                        String line = input.nextLine();
                        System.out.println(line);
                    }
            }
        //closes connection
        input.close();

        }

        catch(Exception e){
            System.err.println(e);

        }



    }




}

使用以下主要方法:

import java.util.GregorianCalendar;

public class test {
public static void main(String[] args){
    GregorianCalendar start = new GregorianCalendar(2015,7,10);
    GregorianCalendar end = new GregorianCalendar(2016,7,10);
    String symbol ="NVO";
    Download test = new Download(symbol,end,start);
    System.out.println("Done");
}

}

1 个答案:

答案 0 :(得分:0)

//http://chart.finance.yahoo.com/table.csv?s=ABCB4.SA&a=1&b=19&c=2017&d=2&e=19&f=2017&g=d&ignore=.csv
    String url = "http://chart.finance.yahoo.com/table.csv?s="+symbol+".SA"+
            "&a="+start.get(Calendar.MONTH)+
            "&b="+start.get(Calendar.DAY_OF_MONTH)+
            "&c="+start.get(Calendar.YEAR)+
            "&d="+end.get(Calendar.MONTH)+
            "&e="+end.get(Calendar.DAY_OF_MONTH)+
            "&f="+end.get(Calendar.YEAR)+
            "&g=d&ignore=.csv";

    System.out.println(url);

    try
    {
        URL yhoofin = new URL(url);
        URLConnection data = yhoofin.openConnection();
        data.connect();//not necessary
        System.out.println("Connection Open! = "+data.getHeaderFields().toString());

        String redirect = data.getHeaderField("Location");
        if (redirect != null){
            data = new URL(redirect).openConnection();
        }
        BufferedReader in = new BufferedReader(new InputStreamReader(data.getInputStream()));
        String inputLine;
        System.out.println();
        in.readLine();//skip first line
        while ((inputLine = in.readLine()) != null) {
            System.out.println(inputLine);
            lines.add(inputLine);
        }