使用C#采集Shibor数据到Excel

对Shibor的变化一直以来比较关注,正好最近学习了对html数据处理的一些知识,就打算拿来采集一些我需要的Shibor数据。

使用到的库

HttpAgilityPack 一个非常不错的html解析工具库

NPOI 一个非常好的office文档生成和读取的库。

整个过程如下:

分析网页内容结构

Shibor的网站开发的比较早了,所以网页布局采用的大量的Table和iFrame,利用浏览器的F12工具,可以很方便的分析出来需要数据的位置。

从上面截图可以看出,我需要的数据区域是一个iFrame加载出来的,所以其实这个src的内容就是我要的,即/shibor/Shibor.do?date=2018-04-25,结合域名知道实际请求url是http://www.shibor.org/shibor/Shibor.do?date=2018-04-25 参数是日期,格式是yyyy-MM-dd,也就是说只需要构造这个参数,就可以得到不同日期的shibor数据了。

将这个新url输入到浏览器里查看

确实是我需要的内容。

下载和解析网页内容

下载该网页的内容就简单了,可以使用很多种方法,我这里直接使用了Http Agility Pack里的HttpWeb类里的Load方法。

涉及到的页面分析这里不罗嗦了,并不难理解,直接贴代码了。

        public List<ShiborModel> Download(DateTime shiborDate)
        {
            string new_url = $"{url}{shiborDate.ToString("yyyy-MM-dd")}";
            List<ShiborModel> shibors = new List<ShiborModel>();
            try
            {
                var web = new HtmlWeb();
                var doc = web.Load(new_url);
                var results = doc.DocumentNode.SelectSingleNode("//table[@class='shiborquxian']");
                foreach (var item in results.Descendants("tr"))
                {
                    var tds = item.Descendants("td").ToList();
                    var name = tds[1].InnerText;
                    var shibor = tds[2].InnerText;
                    var bp = tds[4].InnerText;
                    shibors.Add(new ShiborModel
                    {
                        Name = name,
                        Shibor = double.Parse(shibor),
                        BP = double.Parse(bp)
                    });

                }
            }
            catch (Exception)
            {
                Console.WriteLine($"处理{shiborDate.ToString("yyyyMMdd")}");
            }

            return shibors;
        }

处理解析得到的数据

上面方法返回的是需要数据的集合,而我需要将这些数据输出为excel格式的文件,而.net输出excel的库也有很多,我使用的是国人自己基于openxmlsdk开发的NPOI库,很优秀的库,同样贴出处理的代码。

关于NPOI的使用,可以参考其他人写的文章。

        public void BatchDownLoad(DateTime startDate, DateTime endDate)
        {
            try
            {
                XSSFWorkbook workbook2007 = new XSSFWorkbook();
                workbook2007.CreateSheet("Shibor");
                if (File.Exists(excel))
                {
                    File.Delete(excel);
                }
                FileStream fs = new FileStream(excel, FileMode.CreateNew);
                XSSFSheet sheet = (XSSFSheet)workbook2007.GetSheet("Shibor");
                XSSFRow first_row = (XSSFRow)sheet.CreateRow(0);
                ((XSSFCell)first_row.CreateCell(0)).SetCellValue("日期");
                ((XSSFCell)first_row.CreateCell(1)).SetCellValue("O/N");
                ((XSSFCell)first_row.CreateCell(2)).SetCellValue("1W");
                ((XSSFCell)first_row.CreateCell(3)).SetCellValue("2W");
                ((XSSFCell)first_row.CreateCell(4)).SetCellValue("1M");
                ((XSSFCell)first_row.CreateCell(5)).SetCellValue("3M");
                ((XSSFCell)first_row.CreateCell(6)).SetCellValue("6M");
                ((XSSFCell)first_row.CreateCell(7)).SetCellValue("9M");
                ((XSSFCell)first_row.CreateCell(8)).SetCellValue("1Y");

                var dataformat = workbook2007.CreateDataFormat();
                var style = workbook2007.CreateCellStyle();
                style.DataFormat = dataformat.GetFormat("yyyy-MM-dd");

                int rowNumber = 1;
                DateTime currentDate = startDate;
                while (currentDate < endDate)
                {
                    var shibors = Download(currentDate);
                    if (shibors.Count == 0)
                        continue;

                    XSSFRow temp_row = (XSSFRow)sheet.CreateRow(rowNumber);
                    ((XSSFCell)temp_row.CreateCell(0)).SetCellValue(currentDate.Date);
                    temp_row.GetCell(0).CellStyle = style;

                    ((XSSFCell)temp_row.CreateCell(1)).SetCellValue(shibors[0].Shibor);
                    ((XSSFCell)temp_row.CreateCell(2)).SetCellValue(shibors[1].Shibor);
                    ((XSSFCell)temp_row.CreateCell(3)).SetCellValue(shibors[2].Shibor);
                    ((XSSFCell)temp_row.CreateCell(4)).SetCellValue(shibors[3].Shibor);
                    ((XSSFCell)temp_row.CreateCell(5)).SetCellValue(shibors[4].Shibor);
                    ((XSSFCell)temp_row.CreateCell(6)).SetCellValue(shibors[5].Shibor);
                    ((XSSFCell)temp_row.CreateCell(7)).SetCellValue(shibors[6].Shibor);
                    ((XSSFCell)temp_row.CreateCell(8)).SetCellValue(shibors[7].Shibor);

                    Console.WriteLine($"{currentDate.Date}处理OK");
                    currentDate = currentDate.AddDays(1);
                    rowNumber++;
                }

                workbook2007.Write(fs);
                fs.Close();
                workbook2007.Close();
            }
            catch (Exception)
            {
                throw;
            }
        }

得到结果

图形不是程序生成的,是我用excel的图表功能附加上去的。

因为不是正式项目,所以没有考虑太多开发的规范性,代码只是要求能运行出结果即可。

posted @ 2018-04-25 01:47  xsz1234  阅读(633)  评论(0编辑  收藏  举报