快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

C#从新浪新闻上提取新闻标题

 下面我以新浪军事新闻模块提取军事新闻的标题,将提取到的新闻标题保存到记事本上

邹平ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!

 

  1. static void Main(string[] args) 
  2.       { 
  3.           Stopwatch watch = new Stopwatch(); 
  4.           watch.Start(); 
  5.           WebClient wc = new WebClient(); 
  6.           int count = 0; 
  7.           //正则表达式 
  8.           string regLinks = "
  9. (.+?)(.+?)
  10. "; 
  11.           //由于耗时太久,在这里我只提取新浪100个页面的新闻标题 
  12.           for (int i = 1; i < 100; i++) 
  13.           { 
  14.               //http://roll.mil.news.sina.com.cn/col/zgjq/index_4.shtml 
  15.               string url = @"http://roll.mil.news.sina.com.cn/col/zgjq/index_"+i+".shtml"; 
  16.  
  17.               string html = wc.DownloadString(url); 
  18.               MatchCollection matchs = Regex.Matches(html, regLinks); 
  19.               using (StreamWriter sw = new StreamWriter(@"c:\news.txt", true, Encoding.GetEncoding("gb2312"))) 
  20.               { 
  21.                   foreach (Match match in matchs) 
  22.                   { 
  23.                       if (match.Success) 
  24.                       { 
  25.                           sw.WriteLine(match.Groups[1].Value + "\t" + match.Groups[2].Value); 
  26.                           count++; 
  27.                       } 
  28.                   } 
  29.               } 
  30.           } 
  31.           watch.Stop(); 
  32.           Console.WriteLine("共提取了{0}个新闻标题",count); 
  33.           Console.WriteLine("共计用时:{0}",watch.Elapsed); 
  34.           Console.ReadKey(); 
  35.       } 

朋友们可以提取其他相关网站的新闻标题,但是提取的时候一定要记得找源代码规律,因为

  1. //正则表达式 
  2. string regLinks = "
  3. (.+?)(.+?)
  4. "; 

正则表达式的拼接是根据标题源代码的规律来提取的,如果不找规律,是很难进行提取的。

希望大家可以根据程序来提取其他网站的内容


当前文章:C#从新浪新闻上提取新闻标题
URL网址:http://6mz.cn/article/jeshjg.html

其他资讯