C#.Net使用正則表達式抓取百度百家文章列表


    工作之余,學習了一下正則表達式,鑒於實踐是檢驗真理的唯一標准,於是便寫了一個利用正則表達式抓取百度百家文章的例子,具體過程請看下面源碼:

    一:獲取百度百家網頁內容

 1 public List<string[]> GetUrl()
 2         {
 3             try
 4             {
 5                 string url = "http://baijia.baidu.com/";
 6                 WebRequest webRequest = WebRequest.Create(url);
 7                 WebResponse webResponse = webRequest.GetResponse();
 8                 StreamReader reader = new StreamReader(webResponse.GetResponseStream());
 9                 string result = reader.ReadToEnd();
10                 reader.Close();
11                 webResponse.Close();
12                 return AnalysisHtml(result);
13             }
14             catch (Exception ex)
15             {
16                 throw ex;
17             }
18         }

    二:通過正則表達式篩選

 1 public List<string[]> AnalysisHtml(string htmlContent)
 2         {
 3             List<string[]> list = new List<string[]>();
 4             string strPattern = "<h3><a\\s*.*>(?<Title>[^<]+)</a></h3>.*\\s*<p\\s*class=\"feeds-item-text\">(?<Abstract>[^<]+)<a\\s*href=\"(?<Url>.*)\"\\s*target=\"_blank\"\\s*class=\"feeds-item-more\"\\s*mon=\".*\\s*\">.*\\s*</a></p>";
 5             Regex regex = new Regex(strPattern, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.CultureInvariant);
 6             if (regex.IsMatch(htmlContent))
 7             {
 8                 MatchCollection matchCollection = regex.Matches(htmlContent);
 9                 foreach (Match match in matchCollection)
10                 {
11                     string[] str = new string[3];
12                     str[0] = match.Groups[1].Value;//獲取到的是列表數據的標題
13                     str[1] = match.Groups[2].Value;//獲取到的是內容
14                     str[2] = match.Groups[3].Value;//獲取到的是鏈接到的地址
15                     list.Add(str);
16                 }
17             }
18             return list;
19         }

 源碼下載


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM