public void Fill(string area, string stockCode) { Console.WriteLine("正在处理: " + stockCode); var cwzyLst = new List<CWZY>(); using (var db = new StockContext1()) { var htmlContent = webClient.DownloadString(string.Format(baseUrl, stockCode)); DomFinder _finder = new DomFinder(htmlContent); var table = _finder.Find.AllByTagName<HtmlTable>("table")[19]; for (int i = 0; i < table.Rows.Count;) { if ((i % 13) == 0) { CWZY cwzy = new CWZY(); cwzy.jzrq = Convert.ToDateTime(table.Rows[i].Cells[1].InnerText); cwzy.mgjzc = Convert.ToDecimal(removeLastChar(table.Rows[i + 1].Cells[1].InnerText)); cwzy.mgsy = Convert.ToDecimal(removeLastChar(table.Rows[i + 2].Cells[1].InnerText)); cwzy.mgxjhl = Convert.ToDecimal(removeLastChar(table.Rows[i + 3].Cells[1].InnerText)); cwzy.mgzbgjj = Convert.ToDecimal(removeLastChar(table.Rows[i + 4].Cells[1].InnerText)); cwzy.mgzchj = Convert.ToDecimal(removeLastChar(table.Rows[i + 5].Cells[1].InnerText)); cwzy.ldzchj = Convert.ToDecimal(removeLastChar(table.Rows[i + 6].Cells[1].InnerText)); cwzy.zchj = Convert.ToDecimal(removeLastChar(table.Rows[i + 7].Cells[1].InnerText)); cwzy.cqfzhj = Convert.ToDecimal(removeLastChar(table.Rows[i + 8].Cells[1].InnerText)); cwzy.zyywsr = Convert.ToDecimal(removeLastChar(table.Rows[i + 9].Cells[1].InnerText)); cwzy.cwfy = Convert.ToDecimal(removeLastChar(table.Rows[i + 10].Cells[1].InnerText)); cwzy.jlr = Convert.ToDecimal(removeLastChar(table.Rows[i + 11].Cells[1].InnerText)); cwzy.stockCode = stockCode; cwzy.Id = GuidCombGenerator.NewGuid(); db.T_cwzy.AddObject(cwzy); i += 13; } } db.SaveChanges(); } }
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(); }
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); } } } }
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); } } } }