首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用javascript进行股票报价

使用javascript进行股票报价
EN

Stack Overflow用户
提问于 2011-03-01 11:00:59
回答 6查看 37.6K关注 0票数 9

有谁知道用javascript获取实时或延迟20分钟的股票行情的方法吗?我在http://code.google.com/apis/finance/docs/finance-gadgets.html上看过google api,但是很难找到一个实用的例子。

有没有人用它来获得任何股票的报价,或者找到更好的方法?

谢谢。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2011-03-01 13:13:12

它花了一段时间,但以下是使用雅虎的YQL和jQuery的工作代码:

代码语言:javascript
复制
<script type="text/javascript" src="jquery-1.5.1.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
            var symbol='goog';
            var callback = function(data) {
                    var price=data.query.results.span[0].content;
                    alert('Stock Price: ' + price);
                };

            var url = 'http://query.yahooapis.com/v1/public/yql';
            // this is the lovely YQL query (html encoded) which lets us get the stock price:
            // select * from html where url="http://finance.yahoo.com/q?s=goog" and xpath='//span[@id="yfs_l10_goog"]'
            var data = "q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Ffinance.yahoo.com%2Fq%3Fs%3D" + symbol + "%22%20and%20xpath%3D'%2F%2Fspan%5B%40id%3D%22yfs_l10_" + symbol + "%22%5D'&format=json";
            $.getJSON(url, data, callback);
    });
</script>
票数 7
EN

Stack Overflow用户

发布于 2014-04-08 23:31:24

有点晚了,但我做了一个有效的代码:)快乐的代码man:你可以放入索引,然后显示索引的所有图片和数字

代码语言:javascript
复制
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
<title>IGotMessage - Finances</title>
<link href="stock_chart_yahoo_finance/styles.css" type="text/css" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta content="Get stock quotes, research investments and read business news that help you manage your financial life." name="description">
<meta content="financial Information,investing,investor,market news,stock research,business news,economy,finance,personal finance,quote,loan,student loan,morgage loan,car loan" name="keywords">
<meta content="all" name="robots">

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script language="JavaScript">
function start(){
    getData();
    document.getElementById('link1').click();
}
function getData() {
var url = "http://query.yahooapis.com/v1/public/yql";
var symbol = $("#symbol").val();
var data = encodeURIComponent("select * from yahoo.finance.quotes where symbol in ('" + symbol + "')");

$.getJSON(url, 'q=' + data + "&format=json&diagnostics=true&env=http://datatables.org/alltables.env")
    .done(function (data) {

         $("#name").text("Bid Price: " + data.query.results.quote.Symbol);
         $("#date").text("Bid Price: " + data.query.results.quote.Date);
         $("#time").text("Bid Price: " + data.query.results.quote.LastTradeTime);
         $("#result").text("Bid Price: " + data.query.results.quote.LastTradePriceOnly);
         $("#chg").text("Bid Price: " + data.query.results.quote.PercentChange);
         $("#bid").text("Bid Price: " + data.query.results.quote.LastTradePriceOnly);
         $("#ask").text("Bid Price: " + data.query.results.quote.Ask);
         $("#volume").text("Bid Price: " + data.query.results.quote.Volume);
         $("#high").text("Bid Price: " + data.query.results.quote.HighLimit);
         $("#low").text("Bid Price: " + data.query.results.quote.LowLimit);

         if(data.query.results.quote.PercentChange.indexOf("+") != -1){

            document.getElementById("chg").className = "greenText";
        }
         else{
            //alert(data.query.results.quote.PercentChange);

            document.getElementById("chg").className = "redText";
        }

}).fail(function (jqxhr, textStatus, error) {
    var err = textStatus + ", " + error;
        $("#result").text('Request failed: ' + err);
});
}
function SendRequest()
{

    getData();
    document.getElementById('link1').click();
}   

function CheckEnter(e)
{
    if ((e.keyCode && e.keyCode==13) || (e.which && e.which==13)) {
        return SendRequest();
    }
    return true;
}       

function changeChart(select, item, url, symbol)
{   
    url = url.replace('yhoo',$("#symbol").val());
    symbol = symbol.replace('YHOO',$("#symbol").val());
    var div1d=document.getElementById("div1d_"+item);
    var div5d=document.getElementById("div5d_"+item);
    var div3m=document.getElementById("div3m_"+item);
    var div6m=document.getElementById("div6m_"+item);
    var div1y=document.getElementById("div1y_"+item);
    var div2y=document.getElementById("div2y_"+item);
    var div5y=document.getElementById("div5y_"+item);
    var divMax=document.getElementById("divMax_"+item);
    var divChart=document.getElementById("imgChart_"+item);
    if(div1d==null || div5d==null || div3m==null || div6m==null || div1y==null || div2y==null || div5y==null || divMax==null || divChart==null)
        return;
    div1d.innerHTML="1d";
    div5d.innerHTML="5d";
    div3m.innerHTML="3m";
    div6m.innerHTML="6m";
    div1y.innerHTML="1y";
    div2y.innerHTML="2y";
    div5y.innerHTML="5y";
    divMax.innerHTML="Max";
    var rand_no = Math.random();
    rand_no = rand_no * 100000000;
    switch(select)
    {
    case 0:
        div1d.innerHTML ="<b>1d</b>";
        divChart.src = "http://ichart.finance.yahoo.com/b?s="+symbol+"&"+rand_no;
        break;
    case 1:
        div5d.innerHTML="<b>5d</b>";
        divChart.src = "http://ichart.finance.yahoo.com/w?s="+symbol+"&"+rand_no;
        break;
    case 2:
        div3m.innerHTML="<b>3m</b>";
        divChart.src = "http://chart.finance.yahoo.com/c/3m/"+url+"?"+rand_no;
        break;
    case 3:
        div6m.innerHTML="<b>6m</b>";
        divChart.src = "http://chart.finance.yahoo.com/c/6m/"+url+"?"+rand_no;
        break;
    case 5:
        div2y.innerHTML="<b>2y</b>";
        divChart.src = "http://chart.finance.yahoo.com/c/2y/"+url+"?"+rand_no;
        break;
    case 6:
        div5y.innerHTML="<b>5y</b>";
        divChart.src = "http://chart.finance.yahoo.com/c/5y/"+url+"?"+rand_no;
        break;
    case 7:
        divMax.innerHTML="<b>msx</b>";
        divChart.src = "http://chart.finance.yahoo.com/c/my/"+url+"?"+rand_no;
        break;
    case 4:
    default:
        div1y.innerHTML="<b>1y</b>";
        divChart.src = "http://chart.finance.yahoo.com/c/1y/"+url+"?"+rand_no;
        break;
    }
}
</script>
</head>

<body style="margin: 0px;" bgcolor="#678fc2" onload="start();">

<div align="center" style="background:url(stock_chart_yahoo_finance/main_body.jpg); width:900px; margin:50px auto; padding:20px;">

<table width="90%" border="0" cellpadding="0" cellspacing="0" align="center">
<tbody>
  <tr valign="top">
    <td class="normalText" align="left">
        <input name="txtQuote" id="symbol" onkeypress="return CheckEnter(event);" value="YHOO" type="text" />
        <input name="button" type="button" id="btnQuote" onclick="return SendRequest();" value="Get Quotes" />
        <br />
        <span class="smallText">e.g. &quot;YHOO or YHOO GOOG&quot;</span>

        <div id="service" style="padding:10px 0;">
            <table style="border: 1px solid black;" width="770">
              <tbody>
                <tr style="font-size: 14px; font-family: Arial,Helvetica,sans-serif; font-weight: bold;">
                    <td>Symbol</td>
                    <td>Date</td>
                    <td>Time</td>
                    <td>Trade</td>
                    <td>% Chg</td>
                    <td>Bid</td>
                    <td>Ask</td>
                    <td>Volume</td>
                    <td>High</td>
                    <td>Low</td>
                </tr>
                <tr style="font-family: Arial,Helvetica,sans-serif; font-size: 14px; padding: 0px 2px;">
                    <td id="name">YHOO</td>
                    <td id="date">3/11/2011</td>
                    <td id="time">5:00pm</td>
                    <td id="result">$17.42</td>
                    <td id="chg"><span id="colorText" class="greenText">+0.36(+2.11%)</span></td>
                    <td id="bid">17.30</td>
                    <td id="ask">17.77</td>
                    <td id="volume">19,455,986</td>
                    <td id="high">17.54</td>
                    <td id="low">17.00</td>
                </tr>
            </tbody>
          </table>
          <br />
          <img id="imgChart_0" src="stock_chart_yahoo_finance/yhoo.png" border="0" /><br />
          <a class="linkText" href='javascript:changeChart(0,0,%20&quot;y/yhoo&quot;,%20&quot;YHOO&quot;);'><span id="div1d_0">1d</span></a>&nbsp;&nbsp;
          <a class="linkText" href='javascript:changeChart(1,0,%20&quot;y/yhoo&quot;,%20&quot;YHOO&quot;);'><span id="div5d_0">5d</span></a>&nbsp;&nbsp;
          <a class="linkText" href='javascript:changeChart(2,0,%20&quot;y/yhoo&quot;,%20&quot;YHOO&quot;);'><span id="div3m_0">3m</span></a>&nbsp;&nbsp;
          <a class="linkText" href='javascript:changeChart(3,0,%20&quot;y/yhoo&quot;,%20&quot;YHOO&quot;);'><span id="div6m_0">6m</span></a>&nbsp;&nbsp;
          <a class="linkText" href='javascript:changeChart(4,0,%20&quot;y/yhoo&quot;,%20&quot;YHOO&quot;);'><span id="div1y_0">1y</span></a>&nbsp;&nbsp;
          <a class="linkText" href='javascript:changeChart(5,0,%20&quot;y/yhoo&quot;,%20&quot;YHOO&quot;);'><span id="div2y_0">2y</span></a>&nbsp;&nbsp;
          <a class="linkText" href='javascript:changeChart(6,0,%20&quot;y/yhoo&quot;,%20&quot;YHOO&quot;);'><span id="div5y_0">5y</span></a>&nbsp;&nbsp;
          <a id="link1" class="linkText" href='javascript:changeChart(7,0,%20&quot;y/yhoo&quot;,%20&quot;YHOO&quot;);'><span id="divMax_0"><b>msx</b></span></a>
          <br />
        </div>
    </td>
</tr>
</tbody>

CSS:

代码语言:javascript
复制
                   body 
        { 
          margin:0; 
          padding:0; 
          height:100%;
            }
        h1
        {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: black;
            text-decoration: none;
            font-weight: normal;    
            margin:0 0 0 0;
        }
        .postTitle
        {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 16px;
            color: Blue;
            font-weight: bold;  
            text-decoration: underline;
        }
        .readon
        {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 16px;
            color: Red;
            font-weight: bold;  
            text-decoration: underline; 
        }   
        .titleText 
        {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 16px;
            color: #000;
            text-decoration: none;
            font-weight: bold;  
        }
        .subtitleText 
        {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: #000;
            text-decoration: none;
            font-weight: bold;  
        }
        .normalText 
        {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: #000;
            text-decoration: none;
        }
        .bigText 
        {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 16px;
            color: black;
            font-weight: bold;  
        }
        .grayText 
        {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: Blue;
            text-decoration: underline;
            font-weight: bold;      
        }
        .barText 
        {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: #000;
            padding-top: 10px;
            border-bottom: solid 1px #000;
            text-decoration: none;
            font-weight: bold;  
        }
        .dateText 
        {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: #666;
            text-decoration: none;
            font-weight: bold;      
        }
        .linkText 
        {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: Blue;
        }
        .normalLinkText 
        {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 12px;
            color: Blue;
        }
        .linkGrayText 
        {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: Blue;
        }
        .errorText 
        {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: #ff0000;
            text-decoration: none;
        }
        .greenText 
        {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: rgb(83, 241, 83);
            text-decoration: none;
        }
        .redText 
        {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: Red;
            text-decoration: none;
        }
        .programText 
        {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 12px;
            text-decoration: none;
        }
        .programGrayText 
        {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 12px;
            color: #666;
            text-decoration: none;
        }
        .programGreenText 
        {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 12px;
            color: Green;
            text-decoration: none;
        }
        .smallText 
        {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 11px;
            color: #666;
            text-decoration: none;
        }
        .smallLinkText 
        {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 11px;
            color: Blue;
            text-decoration: underline;
        }
        .greenButton
        {
            margin: 0;
            border: 0;
            background: URL(images/greenbuttonbg.gif) no-repeat;
            height: 21px;
            width: 120px;
            color: #FFFFFF;
            font-family: Arial, Helvetica, Sans-serif;
            font-size: 14px;
            margin: 0px;
            padding-top: 2px;
            padding-bottom: 2px;
            vertical-align:middle;
            overflow: hidden;
            cursor:pointer;
            cursor:hand;
        }
        /*
        Tabs
        */
        :focus { -moz-outline-style: none; }
        #header ul 
        {
            list-style: none;
            padding: 0;
            margin: 0;
        }
        #header li 
        {
            float: left;
            border-left: 1px solid #d0d0d0;
            border-right: 1px solid #212121;
            border-bottom-width: 0;
            margin: 0 0em   
        }
        #header a 
        {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;        
            text-decoration: none;
            display: block;
            background: URL(images/tabbg.gif) repeat;
            padding: 0.2em 1em 0.2em 1em;
            color: white;
            text-align: center;
            font-weight: bold;  
        }
        #header a:hover 
        {
            background: URL(images/tabhoverbg.gif) repeat;
        }
        #header #selected 
        {
            border-color: #E2C068;
            background: #E2C068;
        }
        #header #selected a 
        {
            position: relative;
            top: 1px;
            background: #E2C068;
            color: #000;
            font-weight: bold;  
        }
        #content 
        {
            background: #E2C068;
            clear: both;
            padding: 0.2em 1em 0.2em;
        }
        .bgWhite
        {
            background-color:White;
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: black;
            text-decoration: none;
            font-weight: normal;    
        }   
        .thNormalText
        {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: #000;    
            background-color:#E2C068;
        }   
票数 6
EN

Stack Overflow用户

发布于 2012-01-29 08:10:10

要在html中插入财经Javasript报价,您可以登录Forexpros的网站管理员部分。你只需要复制/复制代码,他们的财务图表就是高质量的。我把它们用在我的网站上。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5150040

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档