如何从浏览器中的C中的URL下载文件?
问题描述:
我想从一个URL下载文件时,这一个:http://download.finance.yahoo/d/quotes.csv?s=YHOO+GOOG+MSFT&f=sl1d1t1c1hgvbap2 当我在我的浏览器,输入这个网址在浏览器中去,该文件会自动下载,因为它应该。 我想要的是无需使用C语言程序在浏览器中下载此文件,我需要此类信息用于金融项目。 我尝试使用libcurl的下载文件,但下载的libcurl对应于这个网址,因为此网址的唯一事情就是开始下载里面是空的,当然HTML页面。 我想这个URL是某种HTTP服务器的方式,但我完全失去了如何获取这个文件。如何从浏览器中的C中的URL下载文件?
谢谢大家提前为你的时间和帮助,请您是否能帮助解释甚至更好的与C语言代码随意这样做,不要害怕太精确。
答
使用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 ****/
非常感谢您的回答。你是对的,你给我的代码完美无缺。再次感谢您抽出宝贵时间回答问题。 – PiggyGenius 2014-11-26 00:19:26
你或许能成为甚至比你已经是更完美的,回答我关于同一主题的其他问题:http://stackoverflow.com/questions/27079147/how-to-retrieve-data-information-from-flash-website# comment42687404_27079147 – PiggyGenius 2014-11-26 00:29:00
如果你可以在命令行使用'curl'来完成,这个方法将会起作用。但我对SWF的内脏一无所知。 – abligh 2014-11-26 07:47:49