public override IList<BsonDocument> GetData(string area, string stockCode) { var docs = new List<BsonDocument>(); for (int i = 2004; i <= 2015; i++) { Console.WriteLine("正在处理: " + stockCode + "," + i); string url = string.Format(baseUrl, stockCode, i); DomFinder _finder = new DomFinder(downloadHtml(url)); var dataTables = _finder.Find.AllByTagName<HtmlTable>("table"); HtmlTable dataTable = null; foreach (var tbl in dataTables) { if (tbl.ID == "BalanceSheetNewTable0") { dataTable = tbl; break; } } if (dataTable == null) { Console.WriteLine("没有找到资产负债表数据," + stockCode + ":" + i); continue; } var strColPrefix = string.Empty; var reportCnt = dataTable.Rows[0].Cells.Count - 1; //第一行数据为报表日期,可以标识出有多少分报表 for (int j = 0; j < reportCnt; j++) { BsonDocument doc = new BsonDocument(); doc.Add(new BsonElement("stockCode", new BsonString(stockCode))); strColPrefix = string.Empty; foreach (var row in dataTable.Rows) { if (row.Cells.Count == (reportCnt + 1)) { doc.Add(new BsonElement(strColPrefix + "_" + row.Cells[0].InnerText, getCellData(row.Cells[j + 1].InnerText))); } else if (row.Cells.Count == 1) { strColPrefix = row.Cells[0].InnerText; } } docs.Add(doc); } } return docs; }
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(); }