c# 自動(dòng)解析頁面中圖片鏈接并下載到本地
發(fā)表時(shí)間:2023-07-19 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]自動(dòng)解析網(wǎng)頁中圖片鏈接并下載到本地做CMS的時(shí)候,需要采集別人的文章但是里面的圖片好多都是動(dòng)態(tài)的,所以需要下載到本地比較好,下面是基本代碼。using System;using System.Tex...
自動(dòng)解析網(wǎng)頁中圖片鏈接并下載到本地
做CMS的時(shí)候,需要采集別人的文章但是里面的圖片好多都是動(dòng)態(tài)的,所以需要下載到本地比較好,下面是基本代碼。
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
namespace zhang.Common
{
public class HanlerFiles
{
private string[] GetImgTag(string htmlStr)
{
Regex regObj = new Regex("", RegexOptions.Compiled RegexOptions.IgnoreCase);
string[] strAry = new string[regObj.Matches(htmlStr).Count];
int i = 0;
foreach (Match matchItem in regObj.Matches(htmlStr))
{
strAry[i] = GetImgUrl(matchItem.Value);
i++;
}
return strAry;
}
private string GetImgUrl(string imgTagStr)
{
string str = "";
Regex regObj = new Regex("http://.+.(?:jpg gif bmp png)", RegexOptions.Compiled RegexOptions.IgnoreCase);
foreach (Match matchItem in regObj.Matches(imgTagStr))
{
str = matchItem.Value;
}
return str;
}
/**////
/// 根椐Html內(nèi)空自動(dòng)識(shí)別圖像文件,并下載到服務(wù)器指定目錄
///
///
///
///
public int SaveUrlPics(ref string strHTML, string path)
{
string[] imgurlAry = GetImgTag(strHTML);
try
{
for (int i = 0; i < imgurlAry.Length; i++)
{
//WebRequest req = WebRequest.Create(imgurlAry[i]);
string preStr = System.DateTime.Now.ToString() + "_";
preStr = preStr.Replace("-", "");
preStr = preStr.Replace(":", "");
preStr = preStr.Replace(" ", "");
WebClient wc = new WebClient();
wc.DownloadFile(imgurlAry[i], HttpContext.Current.Server.MapPath(path) + "/" + preStr + imgurlAry[i].Substring(imgurlAry[i].LastIndexOf("/") + 1));
strHTML = strHTML.Replace(imgurlAry[i], path + preStr + imgurlAry[i].Substring(imgurlAry[i].LastIndexOf("/") + 1));
}
}
catch (Exception ex)
{
//return ex.Message;
}
return imgurlAry.Length;
}
}
}
學(xué)習(xí)教程快速掌握從入門到精通的電腦知識(shí)