现在要在后台把这些网络图片都下载到本地并把文本里图片的URL替换成本地的
有大湿做过没?指点指点 谢了 很急!
2 个解决方案
#1
现在要在后台把这些网络图片都下载到本地并把文本里图片的URL替换成本地的
--------------------------
用正则取出img的SRC,download到本地服务器,然后在把img的src改为你本地的地址。做法很简单。。。。。。 代码自己慢慢写吧。
--------------------------
用正则取出img的SRC,download到本地服务器,然后在把img的src改为你本地的地址。做法很简单。。。。。。 代码自己慢慢写吧。
#2
给你个例子吧
#region 根据CI抓取网页内容
private string Snatch(string cid)
{
string snatchpath = "http://cid-"+cid+snatchurl;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(snatchpath);
request.AllowAutoRedirect = false;
request.Headers.Add(HttpRequestHeader.AcceptLanguage, "zh-cn");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(),Encoding.Default);
string content = reader.ReadToEnd();
reader.Close();
reader.Dispose();
response.Close();
//SnatchName(content);
return content;
}
catch (Exception ex)
{
WebLog.AddSystemException(ex);
return "";
}
}
#endregion
#region 抓取图片地址、保存图片
private string ImgDownLoad(string urlhtml)
{
string imgpath = "";
Regex reg = new Regex("<img id=\"ICc0_usertile\" src=\"(?<g1>.*?)\" errsrc", RegexOptions.IgnoreCase);
MatchCollection mc = reg.Matches(urlhtml); //设定要查找的字符串
if (mc.Count > 0)
{
imgpath = mc[0].Groups["g1"].Value;
}
return imgpath;
//string path = System.Web.HttpContext.Current.Server.MapPath("~/images/");
//string myStringWebResource = null;
//WebClient myWebClient = new WebClient();
//string filename = imgpath.Substring(imgpath.LastIndexOf("/") + 1);
//myStringWebResource = imgpath;
//myWebClient.DownloadFile(myStringWebResource,path+filename);
//return "images/"+filename;
}
#endregion
#1
现在要在后台把这些网络图片都下载到本地并把文本里图片的URL替换成本地的
--------------------------
用正则取出img的SRC,download到本地服务器,然后在把img的src改为你本地的地址。做法很简单。。。。。。 代码自己慢慢写吧。
--------------------------
用正则取出img的SRC,download到本地服务器,然后在把img的src改为你本地的地址。做法很简单。。。。。。 代码自己慢慢写吧。
#2
给你个例子吧
#region 根据CI抓取网页内容
private string Snatch(string cid)
{
string snatchpath = "http://cid-"+cid+snatchurl;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(snatchpath);
request.AllowAutoRedirect = false;
request.Headers.Add(HttpRequestHeader.AcceptLanguage, "zh-cn");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(),Encoding.Default);
string content = reader.ReadToEnd();
reader.Close();
reader.Dispose();
response.Close();
//SnatchName(content);
return content;
}
catch (Exception ex)
{
WebLog.AddSystemException(ex);
return "";
}
}
#endregion
#region 抓取图片地址、保存图片
private string ImgDownLoad(string urlhtml)
{
string imgpath = "";
Regex reg = new Regex("<img id=\"ICc0_usertile\" src=\"(?<g1>.*?)\" errsrc", RegexOptions.IgnoreCase);
MatchCollection mc = reg.Matches(urlhtml); //设定要查找的字符串
if (mc.Count > 0)
{
imgpath = mc[0].Groups["g1"].Value;
}
return imgpath;
//string path = System.Web.HttpContext.Current.Server.MapPath("~/images/");
//string myStringWebResource = null;
//WebClient myWebClient = new WebClient();
//string filename = imgpath.Substring(imgpath.LastIndexOf("/") + 1);
//myStringWebResource = imgpath;
//myWebClient.DownloadFile(myStringWebResource,path+filename);
//return "images/"+filename;
}
#endregion