.net中 网页抓取数据(提取html中的数据,提取table中的数据)

方法一: 

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);
         }
}


相关的网址:

http://www.cnblogs.com/chuncn/archive/2009/09/07/1561564.html

http://www.cnblogs.com/xinzhyu/archive/2008/12/09/1351434.html

http://msdn.microsoft.com/zh-tw/ee787055.aspx


方法四:(对网页中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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值