C#抓取網(wǎng)頁數(shù)據(jù) 解析標(biāo)題描述圖片等信息 去除HTML標(biāo)簽
一、首先將網(wǎng)頁內(nèi)容整個抓取下來,數(shù)據(jù)放在byte[]中(網(wǎng)絡(luò)上傳輸時形式是byte),進一步轉(zhuǎn)化為String,以便于對其操作,實例如下:
private static string GetPageData(string url)
{
if (url == null || url.Trim() == "")
return null;
WebClient wc = new WebClient();
wc.Credentials = CredentialCache.DefaultCredentials;
Byte[] pageData = wc.DownloadData(url);
return Encoding.Default.GetString(pageData);//.ASCII.GetString
}
二、得到了數(shù)據(jù)的字符串形式,然后可以對網(wǎng)頁進行解析了(其實就是對字符串的各種操作和正則表達(dá)式的應(yīng)用):
常用的的解析還有以下幾種:
1.獲取標(biāo)題
Match TitleMatch = Regex.Match(strResponse, "<title>([^<]*)</title>", RegexOptions.IgnoreCase | RegexOptions.Multiline);
title = TitleMatch.Groups[1].Value;
2.獲取描述信息
Match Desc = Regex.Match(strResponse, "<meta name=\"DESCRIPTION\" content=\"([^<]*)\">", RegexOptions.IgnoreCase | RegexOptions.Multiline);
strdesc = Desc.Groups[1].Value;
3.獲取圖片
public class HtmlHelper
{
/// <summary>
/// HTML中提取圖片地址
/// </summary>
public static List<string> PickupImgUrl(string html)
{
Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
MatchCollection matches = regImg.Matches(html);
List<string> lstImg = new List<string>();
foreach (Match match in matches)
{
lstImg.Add(match.Groups["imgUrl"].Value);
}
return lstImg;
}
/// <summary>
/// HTML中提取圖片地址
/// </summary>
public static string PickupImgUrlFirst(string html)
{
List<string> lstImg = PickupImgUrl(html);
return lstImg.Count == 0 ? string.Empty : lstImg[0];
}
}
4.去除Html標(biāo)簽
private string StripHtml(string strHtml)
{
Regex objRegExp = new Regex("<(.|\n)+?>");
string strOutput = objRegExp.Replace(strHtml, "");
strOutput = strOutput.Replace("<", "<");
strOutput = strOutput.Replace(">", ">");
return strOutput;
}
有些例外會使得去除不干凈,所以建議連續(xù)兩次轉(zhuǎn)化。這樣將Html標(biāo)簽轉(zhuǎn)化為了空格。太多連續(xù)的空格會影響之后對字符串的操作。所以再加入這樣的語句:
//把所有空格變?yōu)橐粋€空格
Regex r = new Regex(@"\s+");
wordsOnly = r.Replace(strResponse, " ");
wordsOnly.Trim();
欄 目:C#教程
本文標(biāo)題:C#抓取網(wǎng)頁數(shù)據(jù) 解析標(biāo)題描述圖片等信息 去除HTML標(biāo)簽
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6562.html
您可能感興趣的文章
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10C#實現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10Extjs4如何處理后臺json數(shù)據(jù)中日期和時間
- 01-10C#異步綁定數(shù)據(jù)實現(xiàn)方法
- 01-10C#動態(tài)創(chuàng)建Access數(shù)據(jù)庫及密碼的方法
- 01-10C#使用ADO.Net部件來訪問Access數(shù)據(jù)庫的方法
- 01-10C#獲取網(wǎng)頁源代碼的方法
- 01-10C#實現(xiàn)ComboBox控件顯示出多個數(shù)據(jù)源屬性的方法
- 01-10C#將圖片存放到SQL SERVER數(shù)據(jù)庫中的方法
- 01-10C#.NET實現(xiàn)網(wǎng)頁自動登錄的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 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)控當(dāng)前操作系統(tǒng)已
隨機閱讀
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10C#中split用法實例總結(jié)


