Tom20081
2018-07-24 14:50
采纳率: 100%
浏览 1.6k

使用 C# 采集网站返回的json数据

1、雪球网数据接口使用说明:
https://xueqiu.com/stock/forchartk/stocklist.json?symbol=代码&period=1day&type=复权还是不复权&begin=开始时间时间戳&end=结束时间时间戳&_=结束时间时间戳
地址参数:上海股票在编号前加SH,深圳股票,在编号前加SZ,period代表的时间间隔,复权不复权使用after和before表示,开始时间和结束时间要用时间戳表示。
注意访问下面的网址前要先点击https://xueqiu.com/,进入一次官网,然后在http头中才能记录你的数据,然后才能通过下面的网址获取数据。
2、网址例子、https://xueqiu.com/stock/forchartk/stocklist.json?symbol=SH600756&period=1day&type=before&begin=1478620800000&end=1510126200000&_=1510126200000
3、要求:使用C#完成获取雪球网json数据,需要给出测试结果,demo给出的结果只要symbol(股票代码)volume、open即可

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • blownewbee 2018-07-24 16:12
    已采纳
     using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.IO;
    using System.Web.Script.Serialization;
    
    namespace Q695687
    {
        [Serializable]
        class MyStock
        {
            public string symbol { get; set; }
        }
    
        [Serializable]
        class MyItem
        {
            public string volume { get; set; }
            public string open { get; set; }
        }
    
        [Serializable]
        class MyJson
        {
            public MyStock stock { get; set; }
            public string success { get; set; }
            public List<MyItem> chartlist { get; set; }
        }
        class Program
        {
            static void Main(string[] args)
            {
                WebRequest req = HttpWebRequest.Create("https://xueqiu.com/stock/forchartk/stocklist.json?symbol=SH600756&period=1day&type=before&begin=1478620800000&end=1510126200000&_=1510126200000");
                req.Method = "GET";
                req.Headers.Add("Cookie: aliyungf_tc=AQAAAHToHii4UgkA+PHteUhWTRZyq+2m; xq_a_token=aef774c17d4993658170397fcd0faedde488bd20; xq_a_token.sig=F7BSXzJfXY0HFj9lqXif9IuyZhw; xq_r_token=d694856665e58d9a55450ab404f5a0144c4c978e; xq_r_token.sig=Ozg4Sbvgl2PbngzIgexouOmvqt0; Hm_lvt_1db88642e346389874251b5a1eded6e3=1532446045; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1532446045; u=941532446046068; device_id=ed69614fd948813e0ee1aab73abcdd13");
                var r = req.GetResponse();
                var stream = r.GetResponseStream();
                var reader = new StreamReader(stream, Encoding.UTF8);
                string json = reader.ReadToEnd();
                JavaScriptSerializer jss = new JavaScriptSerializer();
                MyJson result = jss.Deserialize<MyJson>(json);
                Console.WriteLine(result.stock.symbol);
                for (int i = 0; i < 10; i++)
                {
                    Console.WriteLine("\t" + result.chartlist[i].volume + " " + result.chartlist[i].open);
                }
                Console.WriteLine("\t" + "...");
            }
        }
    }
    
    

    图片说明

    点赞 评论
  • blownewbee 2018-07-24 16:27

    作为演示,这里cookie获取我是写死的,你要看到运行效果,需要自己去抓cookie,方法如下:
    图片说明

    用程序获取cookie这个你可以自己搜索下,如果搞不定的话,采纳本回答,我可以详细写给你。另外还可以给你写一个时间/时间戳数字的转换的代码。

    点赞 评论
  • blownewbee 2018-07-24 16:30
    点赞 评论
  • Tom20081 2018-07-25 04:09

    时间戳的方便的话也发一下吧,谢谢

    点赞 评论
  • blownewbee 2018-07-26 00:19

    不好意思,才看到

     using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.IO;
    using System.Web.Script.Serialization;
    
    namespace Q695687
    {
        [Serializable]
        class MyStock
        {
            public string symbol { get; set; }
        }
    
        [Serializable]
        class MyItem
        {
            public string volume { get; set; }
            public string open { get; set; }
        }
    
        [Serializable]
        class MyJson
        {
            public MyStock stock { get; set; }
            public string success { get; set; }
            public List<MyItem> chartlist { get; set; }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://xueqiu.com/");
                req.Method = "GET";
                CookieContainer cc = new CookieContainer();
                req.CookieContainer = cc;
                var rs1 = req.GetResponse().GetResponseStream();
                string cookiestr = req.CookieContainer.GetCookieHeader(req.RequestUri);
                req = (HttpWebRequest)HttpWebRequest.Create("https://xueqiu.com/stock/forchartk/stocklist.json?symbol=SH600756&period=1day&type=before&begin=1478620800000&end=1510126200000&_=1510126200000");
                req.Method = "GET";
                req.Headers.Add("Cookie: " + cookiestr);
                //req.Headers.Add("Cookie: aliyungf_tc=AQAAAHToHii4UgkA+PHteUhWTRZyq+2m; xq_a_token=aef774c17d4993658170397fcd0faedde488bd20; xq_a_token.sig=F7BSXzJfXY0HFj9lqXif9IuyZhw; xq_r_token=d694856665e58d9a55450ab404f5a0144c4c978e; xq_r_token.sig=Ozg4Sbvgl2PbngzIgexouOmvqt0; Hm_lvt_1db88642e346389874251b5a1eded6e3=1532446045; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1532446045; u=941532446046068; device_id=ed69614fd948813e0ee1aab73abcdd13");
                var r = req.GetResponse();
                var stream = r.GetResponseStream();
                var reader = new StreamReader(stream, Encoding.UTF8);
                string json = reader.ReadToEnd();
                JavaScriptSerializer jss = new JavaScriptSerializer();
                MyJson result = jss.Deserialize<MyJson>(json);
                Console.WriteLine(result.stock.symbol);
                for (int i = 0; i < 20; i++)
                {
                    Console.WriteLine("\t" + result.chartlist[i].volume + " " + result.chartlist[i].open);
                }
                Console.WriteLine("\t" + "...");
            }
        }
    }
    
    
    点赞 评论
  • blownewbee 2018-07-26 00:24

    另外给你一个计算时间戳的代码:
    static long gettimestamp(DateTime dt)
    {
    var span = new TimeSpan(dt.Ticks - new DateTime(1970, 1, 1).Ticks);
    return (long)span.TotalMilliseconds;
    }
    使用例子:
    Console.WriteLine(gettimestamp(DateTime.Now));
    结果
    1532593564270

    你可以代入你的begin end的参数
    begin=1478620800000&end=1510126200000

    点赞 评论

相关推荐 更多相似问题