博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.net中 网页抓取数据(提取html中的数据,提取table中的数据)
阅读量:4211 次
发布时间:2019-05-26

本文共 4006 字,大约阅读时间需要 13 分钟。

方法一: 

WebRequest request = WebRequest.Create("http://www.cftea.com/");

 WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));

var contents = reader.ReadToEnd();

Console.WriteLine(StripHTML(contents));

reader.Close();
 reader.Dispose();
response.Close();  
Console.Read();

方法二:(抓取html中table里面的数据)

string html = @"<html>

                            <head>
                            <title></title>
                            </head>
                            <body>
                            <table>a</table>
                            <table>b</table>
                            <table>c</table>
                            <table>d</table>
                            <table>e</table>
                            </body>
                            </html>
                            ";
            var strReg = @"(?is)(?<=<table>).+?(?=</table>)";
            List<string> result = new List<string>();
            MatchCollection mc = Regex.Matches(html, strReg);
            foreach (Match m in mc)
            {
                //result.Add(m.Value);
                Console.WriteLine(m.Value);
            }

方法三:

var htmlDocument = new HtmlDocument();

htmlDocument.LoadHtml(orgStr);
var tables = htmlDocument.DocumentNode.SelectNodes("//table");//xpath的写法
foreach (var table in tables)
{
 foreach (var tr in table.SelectNodes("//tr"))
 {
 var collegeName = tr.SelectNodes("//td").Skip(1).FirstOrDefault().InnerText;
                 Console.WriteLine(collegeName);
         }
}

相关的网址:

方法四:(对网页中table里面的数据提取)

#region http://www.gaokao.com/e/20120109/4f0a8e1773aa0.shtml

            //var url = "http://www.gaokao.com/e/20120109/4f0a8e1773aa0.shtml";
            //var url = "http://www.gaokao.com/e/20120109/4f0a8e1773aa0_2.shtml";
            //var url = "http://www.gaokao.com/e/20120109/4f0a8e1773aa0_3.shtml";
            //var url = "http://www.gaokao.com/e/20120109/4f0a8e1773aa0_4.shtml";
            //var url = "http://www.gaokao.com/e/20120109/4f0a8e1773aa0_5.shtml";
            //var url = "http://www.gaokao.com/e/20120109/4f0a8e1773aa0_6.shtml";
        
            //var orgStr = ChinaEduSp.Crawl.HttpUtility.GetContentByUrl(url, "gb2312");
            //var htmlDocument = new HtmlDocument();
            //htmlDocument.LoadHtml(orgStr);
            //var rows = htmlDocument.DocumentNode.SelectNodes("//table//tr");
            //foreach (var item in rows)
            //{
            //    var pos = item.SelectSingleNode("td[1]").InnerText;
            //    var school = item.SelectSingleNode("td[2]").InnerText;
            //    var province = item.SelectSingleNode("td[3]").InnerText;
            //    var type = item.SelectSingleNode("td[4]").InnerText;
            //    var totalScore = item.SelectSingleNode("td[5]").InnerText;
            //    var seq = db.RankingDescriptions.Count();
                
            //    //Response.Write("名次:" + pos + " 学校名称:" + school + " 所在省份:" + province + " 类型:" + type + " 总分:" + totalScore + "/r/n");
            //    //Response.Write("名次:" + pos + " 学校名称:" + school);
            //    try
            //    {
            //        db.RankingDescriptions.Add(new RankingDescription
            //        {
            //            POS = Convert.ToInt32(pos),
            //            SchoolName = school,
            //            Province = province,
            //            Area = province,
            //            Type = type,
            //            TotalScore = totalScore,
            //            IsShow = true,
            //            IsDelete = false,
            //            RankId = 0,
            //            Seq = seq
            //        });
                    
            //        db.SaveChanges();
            //        seq++;
            //    }
            //    catch (Exception ex)
            //    {
            //        string msg = ex.Message;
            //    }
            //}
            #endregion

#region http://www.gaokao.com/e/20120109/4f0a914934baa_2.shtml

            //var url = "http://www.gaokao.com/e/20120109/4f0a914934baa_2.shtml";
            //var orgStr = ChinaEduSp.Crawl.HttpUtility.GetContentByUrl(url, "gb2312");
            //var htmlDocument = new HtmlDocument();
            //htmlDocument.LoadHtml(orgStr);
            //var rows = htmlDocument.DocumentNode.SelectNodes("//table//tr//td//table//tr");
            //foreach (var item in rows)
            //{
            //    var pos = item.SelectSingleNode("td[1]").InnerText;
            //    var school = item.SelectSingleNode("td[2]").InnerText;
            //    var province = item.SelectSingleNode("td[3]").InnerText;
            //    var totalScore = item.SelectSingleNode("td[4]").InnerText;
            //    var seq = db.RankingDescriptions.Count();
            //    //Response.Write("名次:" + pos + " 学校名称:" + school + " 所在省份:" + province + " 总分:" + totalScore + "/r/n");
            //    //Response.Write("名次:" + pos + " 学校名称:" + school);
            //    try
            //    {
            //        db.RankingDescriptions.Add(new RankingDescription
            //        {
            //            POS = Convert.ToInt32(pos),
            //            SchoolName = school,
            //            Province = province,
            //            Area = province,
            //            TotalScore = totalScore,
            //            IsShow = true,
            //            IsDelete = false,
            //            RankId = 0,
            //            Seq = seq
            //        });
            //        db.SaveChanges();
            //        seq++;
            //    }
            //    catch (Exception ex)
            //    {
            //        string msg = ex.Message;
            //    }
            //}
            #endregion

转载地址:http://sozmi.baihongyu.com/

你可能感兴趣的文章
PowerDesigner 模型文档 说明
查看>>
PowerDesigner 系列 小结
查看>>
Oracle 升级10.2.0.5.4 OPatch 报错Patch 12419392 Optional component(s) missing 解决方法
查看>>
Oracle sessions,processes 和 transactions 参数 关系 说明
查看>>
RMAN 备份报错 RMAN-06207 RMAN-06208 解决方法
查看>>
[INS-35172] Target database memory (XXMB) exceeds the systems available shared memory ({0}MB) 解决方法
查看>>
深入理解 OUI(Oracle Universal Installer)
查看>>
Oracle LOB 详解
查看>>
磁盘性能 -- IOPS 和 吞吐量 说明
查看>>
Oracle Heap size XXK exceeds notification threshold (2048K) 解决方法
查看>>
Oracle Gloden Gate 系列三 -- GG 支持与不支持的对象类型与操作 说明
查看>>
PowerDesigner PDM 生成SQL脚本 去除 引号 方法
查看>>
Oracle Golden Gate 系列四 -- GG 安装 与 卸载 理论知识
查看>>
关系数据库 范式(NF: Normal Form) 说明
查看>>
Oracle Golden Gate 系列五 -- GG 使用配置 说明
查看>>
Oracle Golden Gate 系列六 -- 11gR2 Ora2Ora 单向复制 GG 示例
查看>>
Oracle Golden Gate 系列七 -- 配置 GG Manager process
查看>>
ORA-00600:[32695], [hash aggregation can't be done] 解决方法
查看>>
Oracle SQL中使用正则表达式 执行报ORA-07445 [_intel_fast_memcpy.A()+10] 错误
查看>>
Oracle TABLE ACCESS BY INDEX ROWID 说明
查看>>