简体   繁体   English

读取从url下载的csv数据中的单个数据

[英]Read single data in csv data downloaded from url

I'm trying to fetch the data from Yahoo! 我正在尝试从Yahoo!获取数据。 Finance to get value for currency exchange with this url assuming I'm getting currency exchange for USD to EUR in javascript 假设我要在JavaScript中将美元兑换为EUR,请使用此URL进行货币兑换以获得财务价值

http://download.finance.yahoo.com/d/quotes.csv?s=USDEUR=X&f=l1

in the csv, I would receive just the one value I need. 在csv中,我将只收到所需的一个值。 Eg. 例如。 - '0.8994' -'0.8994'

Now, in same javascript file, I want this value to be pass into a variable 现在,在同一个javascript文件中,我希望将此值传递到变量中

var USDEUR;

at this point, I'm not sure how to pass the value from the downloaded csv. 此时,我不确定如何从下载的csv中传递值。 Could someone enlighten me how to do this properly? 有人可以启发我如何正确地做到这一点吗?

Thanks in advance 提前致谢

It seems you don't actually need to parse this file as a csv, it's basically a plain text string? 看来您实际上不需要将此文件解析为csv,它基本上是纯文本字符串? Assuming jquery: 假设jQuery:

var USDEUR;
$.ajax({
    type: "GET",
    url: "http://download.finance.yahoo.com/d/quotes.csv?s=USDEUR=X&f=l1",
    dataType: "text",
    success: function(data) {USDEUR = data}
 });

EDIT: It seems the url has access control issues, you may not be able to do this with a XHTTP request from the client. 编辑:似乎该URL有访问控制问题,您可能无法通过客户端的XHTTP请求执行此操作。 Is this data meant to be accessed as a public API? 此数据是否打算作为公共API访问?

Use JQuery Ajax 使用JQuery Ajax

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script>
var USDEUR;
$.get("download.finance.yahoo.com/d/quotes.csv?s=USDEUR=X&f=l1", function(data){
  USDEUR = $.parseXML(data);
}, "xml");
</script>

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

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