有時我們需要采集一些信息到自己的數(shù)據(jù)庫,本地磁盤,我們經(jīng)常使用的是WebClient,WebRequest等等,今天主要說一下,對于一個URI地址,采集這個頁面上所有的圖像資源,下面是源代碼,供大家參考,學(xué)習(xí)。
/// summary>
/// 下載指定URL下的所有圖片
/// /summary>
public class WebPageImage
{
/// summary>
/// 獲取網(wǎng)頁中全部圖片
/// /summary>
/// param name="url">網(wǎng)頁地址/param>
/// param name="charSet">網(wǎng)頁編碼,為空自動判斷/param>
/// returns>全部圖片顯示代碼/returns>
public string getImages(string url, string charSet)
{
string s = getHtml(url, charSet);
return getPictures(s, url);
}
/// summary>
/// 獲取網(wǎng)頁中全部圖片
/// /summary>
/// param name="url">網(wǎng)址/param>
/// returns>全部圖片代碼/returns>
public string getImages(string url)
{
return getImages(url, "");
}
string doman(string url)
{
Uri u = new Uri(url);
return u.Host;
}
/// summary>
/// 獲取網(wǎng)頁內(nèi)容
/// /summary>
/// param name="url">網(wǎng)站地址/param>
/// param name="charSet">目標(biāo)網(wǎng)頁的編碼,如果傳入的是null或者"",那就自動分析網(wǎng)頁的編碼 /param>
/// returns>/returns>
string getHtml(string url, string charSet)
{
WebClient myWebClient = new WebClient();
//創(chuàng)建WebClient實例myWebClient
// 需要注意的:
//有的網(wǎng)頁可能下不下來,有種種原因比如需要cookie,編碼問題等等
//這是就要具體問題具體分析比如在頭部加入cookie
// webclient.Headers.Add("Cookie", cookie);
//這樣可能需要一些重載方法。根據(jù)需要寫就可以了
//獲取或設(shè)置用于對向 Internet 資源的請求進行身份驗證的網(wǎng)絡(luò)憑據(jù)。
myWebClient.Credentials = CredentialCache.DefaultCredentials;
//如果服務(wù)器要驗證用戶名,密碼
//NetworkCredential mycred = new NetworkCredential(struser, strpassword);
//myWebClient.Credentials = mycred;
//從資源下載數(shù)據(jù)并返回字節(jié)數(shù)組。(加@是因為網(wǎng)址中間有"/"符號)
byte[] myDataBuffer = myWebClient.DownloadData(url);
string strWebData = Encoding.Default.GetString(myDataBuffer);
//獲取網(wǎng)頁字符編碼描述信息
Match charSetMatch = Regex.Match(strWebData, "meta([^]*)charset=([^]*)\"", RegexOptions.IgnoreCase | RegexOptions.Multiline);
string webCharSet = charSetMatch.Groups[2].Value.Replace("\"", "");
if (charSet == null || charSet == "")
charSet = webCharSet;
if (charSet != null charSet != "" Encoding.GetEncoding(charSet) != Encoding.Default)
strWebData = Encoding.GetEncoding(charSet).GetString(myDataBuffer);
return strWebData;
}
string getPictures(string data, string url)
{
MatchCollection ps = Regex.Matches(data, @"img\b[^>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?imgUrl>[^\s\t\r\n""'>]*)[^>]*?/?[\s\t\r\n]*>");
string s = string.Empty;
for (int i = 0; i ps.Count; i++)
{
pictures p = new pictures(ps[i].Value, url);
s += p.GetHtml + "br />" + Environment.NewLine;
}
return s;
}
/// summary>
/// 圖片實體
/// 圖片文件屬性處理類
/// /summary>
public class pictures
{
public pictures(string strHtml, string baseUrl)
{
_html = strHtml;
Uri u1 = new Uri(baseUrl);
_doman = u1.Host;
_baseUrl = u1.Scheme + "://" + _doman;
setSrc();
}
private string _html = string.Empty;
private string _baseUrl = string.Empty;
private string _doman = string.Empty;
public string GetHtml
{
get { return _html; }
}
public string Alt
{
get
{
return GetAttribute("alt")[0];
}
}
public string Src
{
get
{
string s = GetAttribute("src")[0];
return s;
}
}
/// summary>
/// 根據(jù)基路徑把相對路徑轉(zhuǎn)換成絕對徑
/// /summary>
/// param name="baseUrl">基礎(chǔ)路徑/param>
/// param name="u">待轉(zhuǎn)換的相對路徑/param>
/// returns>絕對路徑/returns>
public string absUrl(string baseUrl, string u)
{
Uri ub = new Uri(baseUrl);
Uri ua = new Uri(ub, u);
return ua.AbsoluteUri;
}
private void setSrc()
{
string strPattern = @"src[\s\t\r\n]*=[\s\t\r\n]*[""']?\S+[""']?";
string src = GetAttribute("src")[0].ToLower();
if (!(src.IndexOf("http://") == 0 || src.IndexOf("https://") == 0) _baseUrl.Length > 10)
{
src = absUrl(_baseUrl, src);
string s = "src=\"" + src + "\"";
_html = Regex.Replace(_html, strPattern, s);
}
}
/// summary>
/// 獲取HTML代碼中標(biāo)簽屬性
/// /summary>
/// param name="strHtml">HTML代碼/param>
/// param name="strAttributeName">屬性名稱/param>
/// returns>屬性值集合/returns>
private string[] GetAttribute(string strAttributeName)
{
Liststring> lstAttribute = new Liststring>();
string strPattern = string.Format(
@"{0}[\s\t\r\n]*=[\s\t\r\n]*[""']?\S+[""']?",
strAttributeName
);
MatchCollection matchs = Regex.Matches(_html, strPattern, RegexOptions.IgnoreCase);
foreach (Match m in matchs)
{
lstAttribute.Add(m.Value.Split('=')[1].Replace("\"", "").Replace("'", ""));
}
if (lstAttribute.Count == 0) lstAttribute.Add("");
return lstAttribute.ToArray();
}
}
}