麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

隨手正則寫的CSDN【只看樓主】功能

2019-11-14 16:44:33
字體:
來源:轉載
供稿:網友

寫這個的時候居然沒有看到原來CSDN已經有這個功能了,寫完代碼了突然發現原來早就已經有了。

現把代碼貼出來吧,雖然有很多解析HTML的開源類庫如:http://htmlagilitypack.codeplex.com/,但我一直習慣于正則匹配。

截圖:

呵呵,起碼還能看吧@——#

  1 PRivate void button1_Click(object sender, EventArgs e)  2         {  3             if (!string.IsNullOrEmpty(txtCsdnUrl.Text.Trim()))  4             {  5                 string url = txtCsdnUrl.Text.Trim();  6                 string htmlSource = string.Empty;  7                 htmlSource = GetHtmlSource(url);  8                 int pageCount = GetPageCount(htmlSource);  9                 string context = string.Empty; 10  11                 if (pageCount > 1) 12                 { 13                     for (int i = 1; i <= pageCount; i++) 14                     { 15                         htmlSource = GetHtmlSource(url + "?page=" + i); 16  17                        context+= GetLZArticle(htmlSource); 18                     } 19                 } 20                 else 21                 { 22                     context += GetLZArticle(htmlSource); 23                 } 24  25                 richTextBox1.Text = context; 26  27             } 28             else 29             { 30                 MessageBox.Show("請輸入地址"); 31             } 32         } 33  34         /// <summary> 35         /// 獲取源代碼 36         /// </summary> 37         /// <param name="Url"></param> 38         /// <returns></returns> 39         public string GetHtmlSource(string Url) 40         { 41             WebClient client = new WebClient(); 42             client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); 43             Stream data = client.OpenRead(Url); 44             string result = string.Empty; 45             using (StreamReader reader = new StreamReader(data, Encoding.UTF8)) 46             { 47                 result = reader.ReadToEnd(); 48             } 49  50             return result; 51         } 52  53         /// <summary> 54         /// 獲取貼子總頁數 URL格式:http://bbs.csdn.net/topics/390730011?page=2 55         /// </summary> 56         /// <returns>返回最大頁數</returns> 57         public int GetPageCount(string HtmlSource) 58         { 59             int pageCount = 0; 60  61             Regex reg = new Regex("<select class=/"jumpMenu/" name=/"jumpMenu/">(?<val>.*?)</select>", RegexOptions.Singleline | RegexOptions.IgnoreCase); 62             string htmlSource = HtmlSource; 63             Regex reg1 = new Regex("<option.*?>(?<val>.*?)</option>", RegexOptions.Singleline | RegexOptions.IgnoreCase); 64             int count = reg1.Matches(reg.Match(htmlSource).Groups["val"].Value).Count; 65  66             int.TryParse(reg1.Matches(reg.Match(htmlSource).Groups["val"].Value)[count - 1].Groups["val"].Value, 67                          out pageCount); 68  69             return pageCount; 70         } 71  72         /// <summary> 73         /// 獲取文章標題 74         /// </summary> 75         /// <param name="HtmlSource">網頁內容</param> 76         /// <returns></returns> 77         public string GetArticleTitle(string HtmlSource) 78         { 79             string title = string.Empty; 80  81             Regex reg = new Regex("<span class=/"title text_overflow/">(?<title>.*?)</span>", RegexOptions.Singleline | RegexOptions.IgnoreCase); 82  83             title = reg.Match(HtmlSource).Groups["title"].Value; 84  85             return title; 86         } 87  88  89         public string GetAuthorName(string HtmlSource) 90         { 91             string result = string.Empty; 92  93             Regex regex = new Regex("<a class=/"p-author/" href=/"#/">(?<value>.*?)</a>"); 94  95             result = regex.Match(HtmlSource).Groups["value"].Value; 96  97             return result; 98         } 99 100         public string GetLZArticle(string HtmlSource)101         {102 103             string result = string.Empty;104             string authorName = GetAuthorName(HtmlSource);105 106             Regex regex = new Regex("<td valign=/"top/" class=/"post_info .*?/" data-username=/"" + authorName + "/".*?>.*?<div class=/"post_body/">(?<value>.*?)</div>.*?</td>", RegexOptions.Singleline | RegexOptions.IgnoreCase);107 108             for (int i = 0; i < regex.Matches(HtmlSource).Count; i++)109             {110                 result += regex.Matches(HtmlSource)[i].Groups["value"].Value;111                 result += "--------------------分隔線--------------------";112             }113             return result.Trim().Replace("<br />","/r/n");114         }

代碼都在這里了。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 色羞羞 | avav在线播放 | 成人福利在线播放 | 成年人高清视频在线观看 | 亚洲综合视频在线播放 | 在线观看一二三 | 538任你躁在线精品视频网站 | 热re91久久精品国产99热 | 在线成人免费观看视频 | 亚洲精品久久久久www | 久草在线视频精品 | 黄网站免费入口 | 天天看夜夜爽 | 国产精品久久久久久久模特 | 一级性生活视频 | www.91sao| 免费黄色在线电影 | 蜜桃视频在线观看免费 | 国产精品伊人久久 | 国产一区二区三区在线视频 | 成人午夜精品久久久久久久3d | 在线看免电影网站 | 黄色淫片 | 99热1| 97超碰资源站 | 久久精热 | asian gaysex| 极色品影院 | 在线成人一区二区 | 黄色特级| 4p嗯啊巨肉寝室调教男男视频 | 天天草夜夜爽 | 精选久久 | 香蕉视频1024 | 久草在线资源视频 | 欧美性成人 | 素人视频免费观看 | 一级毛片在线观看视频 | 久久久久久艹 | 911网站大全在线观看 | 男女生羞羞视频网站在线观看 |