日期:2014-05-18  浏览次数:20430 次

抓取图片相对路径问题
请问怎么在通过HttpWebRequest获取请求网站源文件时将里面的 <img   src= "aaaa.gif "/> 这样的相对路径转换为 <img   src= "http://www.abc.com/aaaa.gif "/> 这样的绝对路径。

------解决方案--------------------
是在获取之后对网页源码字符串进行处理吗,如果是,看下这样是不是你想要的效果

string yourStr = ........;
string startStr = "http://www.abc.com/ ";
string resultStr = Regex.Replace(yourStr, @ "( <img\s[^> ]*src=([ " " ']?))(?!http://)([^ " " '\s> ]*\2?[^> ]*> ) ", "$1 " + startStr + "$3 ", RegexOptions.IgnoreCase);

------解决方案--------------------
Uri mrefr = new Uri(currenturl);
if (m.Groups[1].Value.ToLower().StartsWith( "http "))
{
curclick = m.Groups[1].Value;
}
else if (m.Groups[1].Value.StartsWith( "/ "))
{
curclick = mrefr.Scheme + ":// " + mrefr.Authority + m.Groups[1].Value;
}
else
{
curclick = currenturl.Substring(0, currenturl.LastIndexOf( "/ ")) + "/ " + m.Groups[1].Value;
}
我写得复杂了一些
-----------------------
CSDN 论坛助手
http://china-csdn.cn/downfile/csdn.rar