Yahoo Finance URL not working (again since 05/2018)

时间:2018-06-04 17:17:54

标签: java

Thanks to this valuable site, I found useful tips since 08/2017 to retrieve cookies and crumbs for Yahoo Finance site in order to solve my bulk quote download problem.

Nevertheless my program (written in Java) doesn't work anymore since end of May 2018.
I get the following error message :

CookieHandler retrieved cookie: GUCS="AX62rEgH";$Path="/";$Domain=".yahoo.com" Added cookie using cookie handler getContent on quote failed: java.io.IOException: Server returned HTTP response code: 401 for URL: https://query1.finance.yahoo.com/v7/finance/download/AC.PA?period1=1526594400&period2=1527631200&interval=1d&events=history&crumb=null

I think that the crumb search is failing..

FYI : I am a Java programmer "amateur" since 2003

Please advise if anybody knows how to solve this problem

1 个答案:

答案 0 :(得分:0)

感谢Maxzoom和Dave的快速回答。我为我的问题缺乏详细信息而道歉。因此我在上个月成功使用完整的java方法,一方面感谢Serge于2017年8月27日的代码 由于评论页面中的方法太长,我会粘贴一个新答案以下是方法:

public static String getQuote3(String quoteString, String stock) throws IOException {
    int         curByte;
    char        curChar;
    String      curQuote,z;
    boolean     priceFlag;

    z="rr";
    //////////////Search for cookies

    try {
        CookieManager manager = new CookieManager();
        manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
        CookieHandler.setDefault(manager);
        URL quoteURL = new URL("https://fr.finance.yahoo.com/quote"+stock+"/history?p="+stock);

        URLConnection con = quoteURL.openConnection();
        con.getContent();
        // get cookies from underlying CookieStore
        CookieStore cookieJar = manager.getCookieStore();
        java.util.List <HttpCookie> cookies = cookieJar.getCookies();
        for (HttpCookie cookie: cookies) {
            System.out.println("CookieHandler retrieved cookie: " + cookie);
        }

        //now you can search for the crumb in the yahoo site:
        String crumb = null;
        InputStream inStream = con.getInputStream();
        InputStreamReader irdr = new InputStreamReader(inStream);
        BufferedReader rsv = new BufferedReader(irdr);
        Pattern crumbPattern = Pattern.compile(".*\"CrumbStore\":\\{\"crum\":\"([^\"]+)\"\\}.*");

        String line = null;
        while (crumb == null && (line = rsv.readLine()) != null) {
            Matcher matcher = crumbPattern.matcher(line);
            if (matcher.matches() && matcher.group(1).length()< 12)
            crumb = matcher.group(1);
            if(crumb!= null)
            {
                System.out.println ("crumb= " + crumb) ;
            }
        }
        rsv.close();
        String quoteUrls = quoteString + crumb;

        // create cookie
        HttpCookie cookie = new HttpCookie("UserName", "John Doe");

        // add cookie to CookieStore for a particular URL quoteURL = new URL(quoteUrls);

        try {
            cookieJar.add(quoteURL.toURI(), cookie);
            System.out.println("Added cookie using cookie handler");
        } catch(Exception e) {
            System.out.println("Unable to set cookie using CookieHandler");
            e.printStackTrace();
        }

        con.connect();

        try {
            DataInputStream quoteStream = new DataInputStream(quoteURL.openStream());
            priceFlag = false;
            curQuote = "";
            while( (curByte = quoteStream.read()) != -1) {
                curChar = (char) curByte;

                curQuote += curChar;
            }
            System.out.println(curQuote);
            priceFlagn = true;

            return curQuote;
        } catch (IOException e) {
            System.err.println("getContent on quote failed: " + e);
            priceFlagn = false;
        }

    } catch (MalformedURLException e) {
        System.err.println("Yikes. URL exception");
    }

    return z;
}