asp.net采集頁(yè)面上所有圖像圖片資源的具體方法
有時(shí)我們需要采集一些信息到自己的數(shù)據(jù)庫(kù),本地磁盤(pán),我們經(jīng)常使用的是WebClient,WebRequest等等,今天主要說(shuō)一下,對(duì)于一個(gè)URI地址,采集這個(gè)頁(yè)面上所有的圖像資源,下面是源代碼,供大家參考,學(xué)習(xí)。
/// <summary>
/// 下載指定URL下的所有圖片
/// </summary>
public class WebPageImage
{
/// <summary>
/// 獲取網(wǎng)頁(yè)中全部圖片
/// </summary>
/// <param name="url">網(wǎng)頁(yè)地址</param>
/// <param name="charSet">網(wǎng)頁(yè)編碼,為空自動(dòng)判斷</param>
/// <returns>全部圖片顯示代碼</returns>
public string getImages(string url, string charSet)
{
string s = getHtml(url, charSet);
return getPictures(s, url);
}
/// <summary>
/// 獲取網(wǎng)頁(yè)中全部圖片
/// </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)頁(yè)內(nèi)容
/// </summary>
/// <param name="url">網(wǎng)站地址</param>
/// <param name="charSet">目標(biāo)網(wǎng)頁(yè)的編碼,如果傳入的是null或者"",那就自動(dòng)分析網(wǎng)頁(yè)的編碼 </param>
/// <returns></returns>
string getHtml(string url, string charSet)
{
WebClient myWebClient = new WebClient();
//創(chuàng)建WebClient實(shí)例myWebClient
// 需要注意的:
//有的網(wǎng)頁(yè)可能下不下來(lái),有種種原因比如需要cookie,編碼問(wèn)題等等
//這是就要具體問(wèn)題具體分析比如在頭部加入cookie
// webclient.Headers.Add("Cookie", cookie);
//這樣可能需要一些重載方法。根據(jù)需要寫(xiě)就可以了
//獲取或設(shè)置用于對(duì)向 Internet 資源的請(qǐng)求進(jìn)行身份驗(yàn)證的網(wǎng)絡(luò)憑據(jù)。
myWebClient.Credentials = CredentialCache.DefaultCredentials;
//如果服務(wù)器要驗(yàn)證用戶(hù)名,密碼
//NetworkCredential mycred = new NetworkCredential(struser, strpassword);
//myWebClient.Credentials = mycred;
//從資源下載數(shù)據(jù)并返回字節(jié)數(shù)組。(加@是因?yàn)榫W(wǎng)址中間有"/"符號(hào))
byte[] myDataBuffer = myWebClient.DownloadData(url);
string strWebData = Encoding.Default.GetString(myDataBuffer);
//獲取網(wǎng)頁(yè)字符編碼描述信息
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>
/// 圖片實(shí)體
/// 圖片文件屬性處理類(lèi)
/// </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ù)基路徑把相對(duì)路徑轉(zhuǎn)換成絕對(duì)徑
/// </summary>
/// <param name="baseUrl">基礎(chǔ)路徑</param>
/// <param name="u">待轉(zhuǎn)換的相對(duì)路徑</param>
/// <returns>絕對(duì)路徑</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">屬性名稱(chēng)</param>
/// <returns>屬性值集合</returns>
private string[] GetAttribute(string strAttributeName)
{
List<string> lstAttribute = new List<string>();
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();
}
}
}
調(diào)用:
new WebPageImage().getImages(http://www.sina.com)
結(jié)果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
C#反射技術(shù)的簡(jiǎn)單操作(讀取和設(shè)置類(lèi)的屬性)
反射的作用想必大家都知道了吧,少量屬性的自動(dòng)化操作手動(dòng)添加幾下當(dāng)然是沒(méi)有問(wèn)題的,但是屬性數(shù)量較多的時(shí)候敲起這些繁鎖的代碼可以困了,再說(shuō)對(duì)擴(kuò)展和維護(hù)性造成很多的不遍,以下代碼中如不能直接使用請(qǐng)?zhí)砑觰sing System.Text;的引用。2011-01-01
如何在.Net6 web api中記錄每次接口請(qǐng)求的日志
.net6有自帶的logging組件,還有很多優(yōu)秀的開(kāi)源log組件,如NLog,serilog,這里我們使用serilog組件來(lái)構(gòu)建日志模塊,這篇文章主要介紹了如何在.Net6 web api中記錄每次接口請(qǐng)求的日志,需要的朋友可以參考下2023-06-06
ASP.NET Core針對(duì)一個(gè)使用HttpClient對(duì)象的類(lèi)編寫(xiě)單元測(cè)試詳解
這篇文章主要給大家介紹了關(guān)于A(yíng)SP.NET Core中如何針對(duì)一個(gè)使用HttpClient對(duì)象的類(lèi)編寫(xiě)單元測(cè)試的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
asp.net 讀取并修改config文件實(shí)現(xiàn)代碼
讀取并修改 config 文件的asp.net代碼,方便我們用asp.net修改配置文件。2009-11-11
ASP.NET網(wǎng)站導(dǎo)航及導(dǎo)航控件如何使用
這篇文章主要介紹了ASP.NET網(wǎng)站導(dǎo)航及導(dǎo)航控件如何使用,需要的朋友可以參考下2015-09-09
ASP.NET Core中實(shí)現(xiàn)全局異常攔截的完整步驟
這篇文章主要給大家介紹了關(guān)于A(yíng)SP.NET Core中如何實(shí)現(xiàn)全局異常攔截的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
ASP.NET Core MVC在視圖中使用依賴(lài)注入
這篇文章介紹了ASP.NET Core MVC在視圖中使用依賴(lài)注入的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04

