如何从网页中提取所需的数据

时间:2011-11-07 10:17:04

标签: c# asp.net

大家好,根据我的要求,我想从这个网站提取数据

http://loving1.tea.state.tx.us/lonestar/Menu_dist.aspx?parameter=101902

我想提取网格中显示的数据,我怎么能帮助我

我试过这个

WebRequest request = WebRequest.Create("http://loving1.tea.state.tx.us/lonestar/Menu_dist.aspx?parameter=101902");
    WebResponse response = request.GetResponse();
    Stream data = response.GetResponseStream();
    string html = String.Empty;
    using (StreamReader sr = new StreamReader(data))
    {
        html = sr.ReadToEnd();
   }

enter image description here

我想要提取的网格数据在图像中。请帮忙

2 个答案:

答案 0 :(得分:1)

直截了当的方式 - 下载页面并通过查找适当的<table>标记解析HTML,但是这样每次甚至更改HTML布局或其他任何内容时都必须更新“解析器”...

另一种方法是利用网站提供的“导出到...”功能,这样您就可以使用“导出到Excel 2007按钮”来模拟HTTP请求。想法是Excel 2007工作簿是一个zip存档,包含XML数据文件和CSS样式表。因此,您将能够加载格式良好的XML数据文件/多个文件。

基础网址:

http://loving1.tea.state.tx.us/Common.Cognos/Secured/ReportViewer.aspx?reportSearchPath=/content/folder[@name='TPEIR']/folder[@name='LS']/package[@name='Districts and Schools']/report[@name='AAG5_Dist_Over']&ui.name=AAG5_Dist_Over&year=2010&district=101902&server=Loving1.tea.state.tx.us/lonestar

然后下载XLSX文件,该文件是带有嵌入式XML文件的ZIP存档

  • XL \的工作表\ Sheet1.xml
  • XL \ workbook.xml

所以只需解压缩,加载XML并享受它......

答案 1 :(得分:1)

使用WebClient.DownloadString("http://loving1.tea.state.tx.us/lonestar/Menu_dist.aspx?parameter=101902")从服务器获取数据 而不是使用HTMLAgilityPack来解析html。