Hello Mat

 找回密码
 立即注册
查看: 6826|回复: 0

C#获取新浪实时股票数据

  [复制链接]

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
发表于 2017-5-6 23:24:53 | 显示全部楼层 |阅读模式
C#获取新浪实时股票数据:
参考链接:
http://blog.csdn.net/simon803/article/details/7784682
http://blog.csdn.net/shujudeliu/article/details/40818653
http://outofmemory.cn/code-snipp ... ter-chuan-come-file

UrlTools.cs:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading.Tasks;

  8. namespace WindowsFormsApplication1
  9. {
  10.     public class UrlTools
  11.     {
  12.         /*
  13.          * 0:”大秦铁路”,股票名字;
  14. 1:”27.55″,今日开盘价;
  15. 2:”27.25″,昨日收盘价;
  16. 3:”26.91″,当前价格;
  17. 4:”27.55″,今日最高价;
  18. 5:”26.20″,今日最低价;
  19. 6:”26.91″,竞买价,即“买一”报价;
  20. 7:”26.92″,竞卖价,即“卖一”报价;
  21. 8:”22114263″,成交的股票数,由于股票交易以一百股为基本单位,所以在使用时,通常把该值除以一百;
  22. 9:”589824680″,成交金额,单位为“元”,为了一目了然,通常以“万元”为成交金额的单位,所以通常把该值除以一万;
  23. 10:”4695″,“买一”申请4695股,即47手;
  24. 11:”26.91″,“买一”报价;
  25. 12:”57590″,“买二”
  26. 13:”26.90″,“买二”
  27. 14:”14700″,“买三”
  28. 15:”26.89″,“买三”
  29. 16:”14300″,“买四”
  30. 17:”26.88″,“买四”
  31. 18:”15100″,“买五”
  32. 19:”26.87″,“买五”
  33. 20:”3100″,“卖一”申报3100股,即31手;
  34. 21:”26.92″,“卖一”报价
  35. (22, 23), (24, 25), (26,27), (28, 29)分别为“卖二”至“卖四的情况”
  36. 30:”2008-01-11″,日期;
  37. 31:”15:05:32″,时间;
  38.          *
  39.          *
  40.          * **/
  41.         /// <summary>  
  42.         /// 获取url的返回值  
  43.         /// </summary>  
  44.         /// <param name="url">eg:http://m.weather.com.cn/atad/101010100.html </param>  
  45.         public static string GetInfo(string url)
  46.         {
  47.             string strBuff = "";
  48.             Uri httpURL = new Uri(url);
  49.             ///HttpWebRequest类继承于WebRequest,并没有自己的构造函数,需通过WebRequest的Creat方法 建立,并进行强制的类型转换   
  50.             HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(httpURL);
  51.             ///通过HttpWebRequest的GetResponse()方法建立HttpWebResponse,强制类型转换   
  52.             HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();
  53.             ///GetResponseStream()方法获取HTTP响应的数据流,并尝试取得URL中所指定的网页内容   
  54.             ///若成功取得网页的内容,则以System.IO.Stream形式返回,若失败则产生ProtoclViolationException错 误。在此正确的做法应将以下的代码放到一个try块中处理。这里简单处理   
  55.             Stream respStream = httpResp.GetResponseStream();
  56.             ///返回的内容是Stream形式的,所以可以利用StreamReader类获取GetResponseStream的内容,并以   
  57.             //StreamReader类的Read方法依次读取网页源程序代码每一行的内容,直至行尾(读取的编码格式:UTF8)   
  58.             StreamReader respStreamReader = new StreamReader(respStream, Encoding.Default);
  59.             strBuff = respStreamReader.ReadToEnd();
  60.             return strBuff;
  61.         }
  62.         public static void WriteStringToFile(string fileName, string contents)
  63.         {
  64.             StreamWriter sWriter = null;
  65.             try
  66.             {
  67.                 FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
  68.                 sWriter = new StreamWriter(fileStream);
  69.                 sWriter.Write(contents);
  70.             }
  71.             finally
  72.             {
  73.                 if (sWriter != null)
  74.                 {
  75.                     sWriter.Close();
  76.                 }
  77.             }
  78.         }
  79.         public static void AppendStringToFile(string fileName, string contents)
  80.         {
  81.             StreamWriter sWriter = null;
  82.             try
  83.             {
  84.                 FileStream fileStream = new FileStream(fileName, FileMode.Append, FileAccess.Write);
  85.                 sWriter = new StreamWriter(fileStream);
  86.                 sWriter.Write(contents);
  87.             }
  88.             finally
  89.             {
  90.                 if (sWriter != null)
  91.                 {
  92.                     sWriter.Close();
  93.                 }
  94.             }
  95.         }
  96.     }
  97. }
复制代码

【获取当前行情】代码如下:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;

  11. namespace WindowsFormsApplication1
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         private string baseUrlFormat = "http://hq.sinajs.cn/list={0}";
  16.         private static string rowTitle = "股票名字        今日开盘价        昨日收盘价        当前价格        今日最高价        今日最低价        竞买价        竞卖价        成交的股票数        成交金额        买一        买二        买二        买三        买三        买四        买四        买五        买五        卖一        卖二        卖三        卖四        卖五        日期        时间" + Environment.NewLine;
  17.         private string basePath = @"C:\Users\ysw\Desktop\New Folder99999999999999";
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.         }

  22.         private void btnGetUrlData_Click(object sender, EventArgs e)
  23.         {
  24.             try
  25.             {
  26.                 string stockCode = this.txtUrlName.Text;
  27.                 if (!string.IsNullOrEmpty(stockCode))
  28.                 {
  29.                     string fileNamePath = string.Format("{0}{1}.txt", basePath, stockCode);
  30.                     string url = string.Format(baseUrlFormat, stockCode);
  31.                     string date = UrlTools.GetInfo(url);
  32.                     int startIndex = date.IndexOf('"');
  33.                     int endIndex = date.LastIndexOf('"');
  34.                     string writeData = date.Substring(startIndex + 1, endIndex - startIndex - 1);
  35.                     string[] rowData = writeData.Split(',');
  36.                     writeData = writeData.Replace(",", "\t") + Environment.NewLine;
  37.                     //写入文件
  38.                     if (!File.Exists(fileNamePath))
  39.                     {
  40.                         UrlTools.WriteStringToFile(fileNamePath, rowTitle);
  41.                         this.richTextBox1.AppendText(rowTitle);
  42.                     }
  43.                     UrlTools.AppendStringToFile(fileNamePath, writeData);
  44.                     this.richTextBox1.AppendText(writeData);
  45.                     this.txtCurrentPrice.Text = rowData[3];
  46.                     MessageBox.Show("OK");
  47.                 }
  48.             }
  49.             catch (Exception ex)
  50.             {
  51.                 MessageBox.Show(ex.Message);
  52.             }
  53.         }
  54.     }
  55. }
复制代码

C#源代码如下:链接:http://pan.baidu.com/s/1pLPnxzl 密码:mjgi

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-4-25 09:15 , Processed in 0.232315 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表