示例#1
0
        static void Main(string[] args)
        {
            //由于我本地之前已经将A股所有公司代码下载下来,所以直接取的是我的数据库
            //您可以将取股票代码的这部分源码替换使用本文件中下边注释部分的源码
            //其中沪深股市代码分别在stockItems_sh.txt和stockItems_sz.txt中

            var sql = "select *  from StockItem order by code";
            var lirb = new LiRunbiao();
            var xjllb = new XianjinliuliangBiao();
            var zifzb = new ZiChanFuzhaiBiao();

              var mongoClient = new MongoClient("mongodb://*****:*****@"c:\a.txt");

            Regex regex = new Regex("\\<li.*href=\"(.*)\">(.*)\\</.*/li");
            WebClient client = new WebClient();

            foreach (Match match in regex.Matches(ash))
            {
                var href = match.Groups[1].Value;
                var html = client.DownloadString(href);

                var table = new DomFinder(html).Find.ByExpression<HtmlTable>("id=wahaha", "|", "tagIndex=table:0");

                if (table != null)
                {
                    Console.WriteLine("Found it");
                }
            }

            Console.WriteLine("Done");
            Console.Read();
        }
示例#2
0
        public void Execute()
        {
            var baseUrl = "http://q.stock.sohu.com/hisHq?code=cn_{0}&start=19910101&end=20150424&stat=1&order=D&period=d&callback=historySearchHandler&rt=jsonp&r=0.4286239536013454&0.14818861591629684";

            var sql = "select * from StockItem a where  not exists (select 1 from [DayData] where StockCode = a.Code and Area = a.Area )";

            using (var db = new StockContext1())
            {

                //   db.Configuration.AutoDetectChangesEnabled = false;
                // db.Configuration.ValidateOnSaveEnabled = false;

                var stocks = db.ExecuteStoreQuery<StockItem>(sql).ToList();
                WebClient client = new WebClient();
                var jsonpFuncLen = "historySearchHandler(".Length;

                foreach (var stock in stocks)
                {
                    Console.WriteLine("Downloading " + stock.Code + "." + stock.Area);

                    try
                    {
                        var jsonpContent = client.DownloadString(string.Format(baseUrl, stock.Code));
                        Console.WriteLine("Download done.");

                        if (string.IsNullOrEmpty(jsonpContent))
                        {
                            continue;
                        }

                        var jsonStr = jsonpContent.Substring(jsonpFuncLen, jsonpContent.Length - jsonpFuncLen - 2);

                        var jsonObj = JsonConvert.DeserializeObject<SohuStockInfo[]>(jsonStr);

                        if (jsonObj[0].status != 0)
                        {
                            continue;
                        }

                        Console.WriteLine("Begin commit");
                        foreach (var array in jsonObj[0].hq)
                        {

                            var stockData = new DayData()
                            {
                                Date = DateTime.Parse(array[0]),
                                Open = decimal.Parse(array[1]),
                                High = decimal.Parse(array[6]),
                                Low = decimal.Parse(array[5]),
                                Close = decimal.Parse(array[2]),
                                Volume = decimal.Parse(array[7]),
                                //AdjClose = decimal.Parse(array[6]),
                                CreateAt = DateTime.Now,
                                Area = stock.Area,
                                StockCode = stock.Code
                            };

                            db.DayData.AddObject(stockData);

                        }

                        db.SaveChanges();
                        Console.WriteLine("Commit done");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }

                }

            }
        }
示例#3
0
        public void Execute()
        {
            var baseUrl = "http://ichart.finance.yahoo.com/table.csv?ignore=.csv&s=";

            var sql = "select * from StockItem a where  not exists (select 1 from [DayData] where StockCode = a.Code and Area = a.Area )";

            using (var db = new StockContext1())
            {
                var stocks = db.ExecuteStoreQuery<StockItem>(sql);
                WebClient client = new WebClient();

                foreach (var stock in stocks)
                {

                    Console.WriteLine("Downloading " + stock.Code + "." + stock.Area);
                    try
                    {
                        var csvContent = client.DownloadString(baseUrl + stock.Code + "." + stock.Area);
                        Console.WriteLine("Download done.");

                        if (string.IsNullOrEmpty(csvContent))
                        {
                            continue;
                        }

                        var index = 0;
                        foreach (var line in csvContent.Split('\r', '\n'))
                        {
                            if (index++ != 0 && !string.IsNullOrEmpty(line))
                            {
                                var array = line.Split(',');

                                var stockData = new DayData()
                                {
                                    Date = DateTime.Parse(array[0]),
                                    Open = decimal.Parse(array[1]),
                                    High = decimal.Parse(array[2]),
                                    Low = decimal.Parse(array[3]),
                                    Close = decimal.Parse(array[4]),
                                    Volume = decimal.Parse(array[5]),
                                    AdjClose = decimal.Parse(array[6]),
                                    CreateAt = DateTime.Now,
                                    Area = stock.Area,
                                    StockCode = stock.Code
                                };

                                db.DayData.AddObject(stockData);
                            }
                        }

                        db.SaveChanges();

                        //var objectStateEntries = db
                        //     .ObjectStateManager
                        //     .GetObjectStateEntries(EntityState.Added);

                        //foreach (var objectStateEntry in objectStateEntries)
                        //{
                        //    db.Detach(objectStateEntry);
                        //}

                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }

                }

            }
        }