C#獲取HTML文本的第一張圖片與截取內(nèi)容摘要示例代碼
獲取第一張圖片
要我們獲得到的數(shù)據(jù)是一段HTML文本、也許這段文本里面有許多圖片、需要截取一張作為標題圖片、也就是做為主圖、這時就可以用到下面這個方法獲取到第一張圖片。
示例代碼
#region 獲取第一張圖片 /// <summary> /// 獲取HTML文本的圖片地址 /// </summary> /// <param name="content"></param> /// <returns></returns>/ /// public ArrayList getimgurl(string html) { ArrayList resultStr = new ArrayList(); Regex r = new Regex(@"<IMG[^>] src=s*(?:´(?<src>[^´] )´|""(?<src>[^""] )""|(?<src>[^>s] ))s*[^>]*>", RegexOptions.IgnoreCase);//忽視大小寫 MatchCollection mc = r.Matches(html); foreach (Match m in mc) { resultStr.Add(m.Groups["src"].Value.ToLower()); } if (resultStr.Count > 0) { return resultStr; } else { resultStr.Clear(); return resultStr; } } #endregion
注意:上面所返回的是一個ArrayList
集合、包含了文本里面所有的Img的src
、這樣我們就可以訪問到img的src
了
截取HTML文本
有時候我們得到的數(shù)據(jù)是一段HTML文本、需要截取HTML文本的一部分作為內(nèi)容摘要、此時、我們可以運用下面這個方法
示例代碼
#region 新聞內(nèi)容摘要 /// <summary> /// 新聞內(nèi)容摘要 /// </summary> /// <param name="sString"></param> /// <param name="nLeng"></param> /// <returns></returns> public string GetContentSummary(string content, int length, bool StripHTML) { if (string.IsNullOrEmpty(content) || length == 0) return ""; if (StripHTML) { Regex re = new Regex("<[^>]*>"); content = re.Replace(content, ""); content = content.Replace(" ", "").Replace(" ", ""); if (content.Length <= length) return content; else return content.Substring(0, length) "……"; } else { if (content.Length <= length) return content; int pos = 0, npos = 0, size = 0; bool firststop = false, notr = false, noli = false; StringBuilder sb = new StringBuilder(); while (true) { if (pos >= content.Length) break; string cur = content.Substring(pos, 1); if (cur == "<") { string next = content.Substring(pos 1, 3).ToLower(); if (next.IndexOf("p") == 0 && next.IndexOf("pre") != 0) { npos = content.IndexOf(">", pos) 1; } else if (next.IndexOf("/p") == 0 && next.IndexOf("/pr") != 0) { npos = content.IndexOf(">", pos) 1; if (size < length) sb.Append("<br/>"); } else if (next.IndexOf("br") == 0) { npos = content.IndexOf(">", pos) 1; if (size < length) sb.Append("<br/>"); } else if (next.IndexOf("img") == 0) { npos = content.IndexOf(">", pos) 1; if (size < length) { sb.Append(content.Substring(pos, npos - pos)); size = npos - pos 1; } } else if (next.IndexOf("li") == 0 || next.IndexOf("/li") == 0) { npos = content.IndexOf(">", pos) 1; if (size < length) { sb.Append(content.Substring(pos, npos - pos)); } else { if (!noli && next.IndexOf("/li") == 0) { sb.Append(content.Substring(pos, npos - pos)); noli = true; } } } else if (next.IndexOf("tr") == 0 || next.IndexOf("/tr") == 0) { npos = content.IndexOf(">", pos) 1; if (size < length) { sb.Append(content.Substring(pos, npos - pos)); } else { if (!notr && next.IndexOf("/tr") == 0) { sb.Append(content.Substring(pos, npos - pos)); notr = true; } } } else if (next.IndexOf("td") == 0 || next.IndexOf("/td") == 0) { npos = content.IndexOf(">", pos) 1; if (size < length) { sb.Append(content.Substring(pos, npos - pos)); } else { if (!notr) { sb.Append(content.Substring(pos, npos - pos)); } } } else { npos = content.IndexOf(">", pos) 1; sb.Append(content.Substring(pos, npos - pos)); } if (npos <= pos) npos = pos 1; pos = npos; } else { if (size < length) { sb.Append(cur); size ; } else { if (!firststop) { sb.Append("……"); firststop = true; } } pos ; } } return sb.ToString(); } } #endregion
總結(jié)
以上就是利用C#獲取一段HTML文本中的第一張圖片和截取內(nèi)容摘要的全部內(nèi)容,希望本文的內(nèi)容對大家學習或者使用C#能有所幫助,如果有疑問大家可以留言交流,謝謝大家對我們的支持。
上一篇:C# 清除cookies的代碼
欄 目:C#教程
本文標題:C#獲取HTML文本的第一張圖片與截取內(nèi)容摘要示例代碼
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6205.html
您可能感興趣的文章
- 01-10C#通過反射獲取當前工程中所有窗體并打開的方法
- 01-10C#獲取進程或線程相關(guān)信息的方法
- 01-10C#調(diào)用dos窗口獲取相關(guān)信息的方法
- 01-10C#編程獲取資源文件中圖片的方法
- 01-10C#獲取任務(wù)欄顯示進程的方法
- 01-10C#及WPF獲取本機所有字體和顏色的方法
- 01-10C#獲取動態(tài)生成的CheckBox值
- 01-10C#獲取網(wǎng)頁源代碼的方法
- 01-10C#獲取客戶端相關(guān)信息實例總結(jié)
- 01-10C#實現(xiàn)獲取不同對象中名稱相同屬性的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10C#中split用法實例總結(jié)
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 04-02jquery與jsp,用jquery