C#獲取HTML文本的第一張圖片與截取內(nèi)容摘要示例代碼
獲取第一張圖片
要我們獲得到的數(shù)據(jù)是一段HTML文本、也許這段文本里面有許多圖片、需要截取一張作為標(biāo)題圖片、也就是做為主圖、這時(shí)就可以用到下面這個(gè)方法獲取到第一張圖片。
示例代碼
#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
注意:上面所返回的是一個(gè)ArrayList 集合、包含了文本里面所有的Img的src、這樣我們就可以訪問到img的src了
截取HTML文本
有時(shí)候我們得到的數(shù)據(jù)是一段HTML文本、需要截取HTML文本的一部分作為內(nèi)容摘要、此時(shí)、我們可以運(yùn)用下面這個(gè)方法
示例代碼
#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)容摘要的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家學(xué)習(xí)或者使用C#能有所幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)我們的支持。
上一篇:C# 清除cookies的代碼
欄 目:C#教程
本文標(biāo)題:C#獲取HTML文本的第一張圖片與截取內(nèi)容摘要示例代碼
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6205.html
您可能感興趣的文章
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法
- 01-10C#獲取進(jìn)程或線程相關(guān)信息的方法
- 01-10C#調(diào)用dos窗口獲取相關(guān)信息的方法
- 01-10C#編程獲取資源文件中圖片的方法
- 01-10C#獲取任務(wù)欄顯示進(jìn)程的方法
- 01-10C#及WPF獲取本機(jī)所有字體和顏色的方法
- 01-10C#獲取動(dòng)態(tài)生成的CheckBox值
- 01-10C#獲取網(wǎng)頁(yè)源代碼的方法
- 01-10C#獲取客戶端相關(guān)信息實(shí)例總結(jié)
- 01-10C#實(shí)現(xiàn)獲取不同對(duì)象中名稱相同屬性的方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 04-02jquery與jsp,用jquery


