使用C#獲取網(wǎng)頁HTML源碼的例子
更新時間:2014年08月13日 10:07:12 投稿:junjie
這篇文章主要介紹了使用C#獲取網(wǎng)頁HTML源碼的例子,考慮了區(qū)分GB2312和UTF-8編碼,需要的朋友可以參考下
最近在做一個項目,其中一個功能是根據(jù)一個URL地址,獲取到網(wǎng)頁的源代碼。在ASP.NET(C#)中,獲取網(wǎng)頁源代碼貌似有很多種方法,我隨便搞了一個簡單的WebClient,非常簡單容易。但后面一個非常惱火的問題出來了,那就是中文的亂碼。
通過仔細(xì)研究,中文的網(wǎng)頁不外乎GB2312和UTF-8這兩種編碼。于是有了下面這段代碼:
復(fù)制代碼 代碼如下:
/// <summary>
/// 根據(jù)網(wǎng)址的URL,獲取源代碼HTML
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string GetHtmlByUrl(string url)
{
using (WebClient wc = new WebClient())
{
try
{
wc.UseDefaultCredentials = true;
wc.Proxy = new WebProxy();
wc.Proxy.Credentials = CredentialCache.DefaultCredentials;
wc.Credentials = System.Net.CredentialCache.DefaultCredentials;
byte[] bt = wc.DownloadData(url);
string txt = System.Text.Encoding.GetEncoding("GB2312").GetString(bt);
switch (GetCharset(txt).ToUpper())
{
case "UTF-8":
txt = System.Text.Encoding.UTF8.GetString(bt);
break;
case "UNICODE":
txt = System.Text.Encoding.Unicode.GetString(bt);
break;
default:
break;
}
return txt;
}
catch (Exception ex)
{
return null;
}
}
}
稍微解釋一下,這里使用了WebClient創(chuàng)建了一個wc對象(這命名有點尷尬了)。然后調(diào)用wc對象的DownloadData方法,傳入URL值,返回一個字節(jié)數(shù)組。默認(rèn)使用GB2312來讀取這個字節(jié)數(shù)組,把它轉(zhuǎn)換成字符串。從網(wǎng)頁源代碼的字符串中查找網(wǎng)頁的編碼格式的特征字符,如找到charset="utf-8"這樣的信息,來判斷當(dāng)前網(wǎng)頁的編碼格式。
GetCharset這個函數(shù)就是來獲取當(dāng)前網(wǎng)頁的編碼格式的,具體代碼如下:
復(fù)制代碼 代碼如下:
/// <summary>
/// 從HTML中獲取獲取charset
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public static string GetCharset(string html)
{
string charset = "";
Regex regCharset = new Regex(@"content=[""'].*\s*charset\b\s*=\s*""?(?<charset>[^""']*)", RegexOptions.IgnoreCase);
if (regCharset.IsMatch(html))
{
charset = regCharset.Match(html).Groups["charset"].Value;
}
if (charset.Equals(""))
{
regCharset = new Regex(@"<\s*meta\s*charset\s*=\s*[""']?(?<charset>[^""']*)", RegexOptions.IgnoreCase);
if (regCharset.IsMatch(html))
{
charset = regCharset.Match(html).Groups["charset"].Value;
}
}
return charset;
}
/// 從HTML中獲取獲取charset
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public static string GetCharset(string html)
{
string charset = "";
Regex regCharset = new Regex(@"content=[""'].*\s*charset\b\s*=\s*""?(?<charset>[^""']*)", RegexOptions.IgnoreCase);
if (regCharset.IsMatch(html))
{
charset = regCharset.Match(html).Groups["charset"].Value;
}
if (charset.Equals(""))
{
regCharset = new Regex(@"<\s*meta\s*charset\s*=\s*[""']?(?<charset>[^""']*)", RegexOptions.IgnoreCase);
if (regCharset.IsMatch(html))
{
charset = regCharset.Match(html).Groups["charset"].Value;
}
}
return charset;
}
感覺寫得不是很好,先將就著用,呵呵。小編原創(chuàng),轉(zhuǎn)載注明,呵呵。
您可能感興趣的文章:
- C#實現(xiàn)將HTML轉(zhuǎn)換成純文本的方法
- ASP.net(c#) 生成html的幾種解決方案[思路]
- C#將html table 導(dǎo)出成excel實例
- C#下解析HTML的兩種方法介紹
- asp.net(C#) 動態(tài)添加非ASP的標(biāo)準(zhǔn)html控件(如添加Script標(biāo)簽)
- C#導(dǎo)出生成excel文件的方法小結(jié)(xml,html方式)
- c#中過濾html的正則表達(dá)式
- C#正則表達(dá)式匹配HTML中的圖片路徑,圖片地址代碼
- C#實現(xiàn)下載網(wǎng)頁HTML源碼的方法
- C#獲取HTML文本的第一張圖片與截取內(nèi)容摘要示例代碼
相關(guān)文章
Unity3D使用陀螺儀控制節(jié)點旋轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了Unity3D使用陀螺儀控制節(jié)點旋轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11
使用C#編寫簡單的圖形化的可發(fā)送附件的郵件客戶端程序
這篇文章主要介紹了使用C#編寫一個圖形化的可發(fā)送附件的郵件客戶端程序的方法,文中的示例同樣是基于支持smtp協(xié)議的郵件服務(wù)器,需要的朋友可以參考下2016-02-02

