日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

随手正则写的 CSDN【只看楼主】功能

發(fā)布時(shí)間:2025/4/9 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 随手正则写的 CSDN【只看楼主】功能 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

寫這個(gè)的時(shí)候居然沒(méi)有看到原來(lái)CSDN已經(jīng)有這個(gè)功能了,寫完代碼了突然發(fā)現(xiàn)原來(lái)早就已經(jīng)有了。

現(xiàn)把代碼貼出來(lái)吧,雖然有很多解析HTML的開源類庫(kù)如:http://htmlagilitypack.codeplex.com/,但我一直習(xí)慣于正則匹配。

截圖:

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

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("請(qǐng)輸入地址"); 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 /// 獲取貼子總頁(yè)數(shù) URL格式:http://bbs.csdn.net/topics/390730011?page=2 55 /// </summary> 56 /// <returns>返回最大頁(yè)數(shù)</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 /// 獲取文章標(biāo)題 74 /// </summary> 75 /// <param name="HtmlSource">網(wǎng)頁(yè)內(nèi)容</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 }

代碼都在這里了。

轉(zhuǎn)載于:https://www.cnblogs.com/pandait/p/CSDN_See_LouZhu.html

總結(jié)

以上是生活随笔為你收集整理的随手正则写的 CSDN【只看楼主】功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。