2014-11-23 136 views
1

我想從一個URL下載文件時,這一個:http://download.finance.yahoo/d/quotes.csv?s=YHOO+GOOG+MSFT&f=sl1d1t1c1hgvbap2 當我在我的瀏覽器,輸入這個網址在瀏覽器中去,該文件會自動下載,因爲它應該。 我想要的是無需使用C語言程序在瀏覽器中下載此文件,我需要此類信息用於金融項目。 我嘗試使用libcurl的下載文件,但下載的libcurl對應於這個網址,因爲此網址的唯一事情就是開始下載裏面是空的,當然HTML頁面。 我想這個URL是某種HTTP服務器的方式,但我完全失去了如何獲取這個文件。如何從瀏覽器中的C中的URL下載文件?

謝謝大家提前爲你的時間和幫助,請您是否能幫助解釋甚至更好的與C語言代碼隨意這樣做,不要害怕太精確。

回答

5

使用libcurl,看this examples page

如果您想讓它工作,請使用命令行curl,並使用--libcurl選項。我懷疑這個問題可能更多的是與JavaScript,cookies,登錄等有關。所有這些都是可以解決的,但是可以使用命令行來使其運行。我的診斷是你的網址在yahoo之後缺少.com

例如:

curl --silent --libcurl /tmp/test.c 'http://download.finance.yahoo.com/d/quotes.csv?s=YHOO+GOOG+MSFT&f=sl1d1t1c1hgvbap2' 

產生輸出到屏幕:

"YHOO",51.04,"11/21/2014","4:00pm",-0.21,52.25,50.99,22226984,N/A,52.49,"-0.41%" 
"GOOG",537.50,"11/21/2014","4:00pm",+2.67,542.14,536.56,2218249,N/A,575.00,"+0.50%" 
"MSFT",47.98,"11/21/2014","4:00pm",-0.72,49.05,47.57,42884796,N/A,49.05,"-1.48%" 

,併產生代碼:

/********* Sample code generated by the curl command line tool ********** 
* All curl_easy_setopt() options are documented at: 
* http://curl.haxx.se/libcurl/c/curl_easy_setopt.html 
************************************************************************/ 
#include <curl/curl.h> 

int 
main (int argc, char *argv[]) 
{ 
    CURLcode ret; 
    CURL *hnd; 

    hnd = curl_easy_init(); 
    curl_easy_setopt (hnd, CURLOPT_URL, 
        "http://download.finance.yahoo.com/d/quotes.csv?s=YHOO+GOOG+MSFT&f=sl1d1t1c1hgvbap2"); 
    curl_easy_setopt (hnd, CURLOPT_NOPROGRESS, 1L); 
    curl_easy_setopt (hnd, CURLOPT_USERAGENT, "curl/7.35.0"); 
    curl_easy_setopt (hnd, CURLOPT_MAXREDIRS, 50L); 
    curl_easy_setopt (hnd, CURLOPT_TCP_KEEPALIVE, 1L); 

    /* Here is a list of options the curl code used that cannot get generated 
    as source easily. You may select to either not use them or implement 
    them yourself. 

    CURLOPT_WRITEDATA set to a objectpointer 
    CURLOPT_WRITEFUNCTION set to a functionpointer 
    CURLOPT_READDATA set to a objectpointer 
    CURLOPT_READFUNCTION set to a functionpointer 
    CURLOPT_SEEKDATA set to a objectpointer 
    CURLOPT_SEEKFUNCTION set to a functionpointer 
    CURLOPT_ERRORBUFFER set to a objectpointer 
    CURLOPT_STDERR set to a objectpointer 
    CURLOPT_HEADERFUNCTION set to a functionpointer 
    CURLOPT_HEADERDATA set to a objectpointer 

    */ 

    ret = curl_easy_perform (hnd); 

    curl_easy_cleanup (hnd); 
    hnd = NULL; 

    return (int) ret; 
} 

/**** End of sample code ****/ 
+0

非常感謝您的回答。你是對的,你給我的代碼完美無缺。再次感謝您抽出寶貴時間回答問題。 – PiggyGenius 2014-11-26 00:19:26

+0

你或許能成爲甚至比你已經是更完美的,回答我關於同一主題的其他問題:http://stackoverflow.com/questions/27079147/how-to-retrieve-data-information-from-flash-website# comment42687404_27079147 – PiggyGenius 2014-11-26 00:29:00

+0

如果你可以在命令行使用'curl'來完成,這個方法將會起作用。但我對SWF的內臟一無所知。 – abligh 2014-11-26 07:47:49