自动识别HTML代码里的图片链接,并下载到服务器的指定目录源码

本文介绍了一种使用C#从HTML中自动识别并下载图片的方法,通过正则表达式匹配图片链接,并将这些图片资源保存到本地服务器上。

做新闻文章,我们或多或少地转载别人的成果,那怎么样把图片也下载到自已的服务器里呢?我这有一段代码,敬请指教!

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(&quot;<img.+?>&quot;, 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 = &quot;&quot;;
            Regex regObj = new Regex(&quot;http://.+.(?:jpg|gif|bmp|png)&quot;, RegexOptions.Compiled | RegexOptions.IgnoreCase);
            foreach (Match matchItem in regObj.Matches(imgTagStr))
            {
                str = matchItem.Value;
            }
            return str;
        }
        /**//// <summary>
        /// 根椐Html内空自动识别图像文件,并下载到服务器指定目录
        /// </summary>
        /// <param name=&quot;strHTML&quot;></param>
        /// <param name=&quot;path&quot;></param>
        /// <returns></returns>
        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() + &quot;_&quot;;
                    preStr = preStr.Replace(&quot;-&quot;, &quot;&quot;);
                    preStr = preStr.Replace(&quot;:&quot;, &quot;&quot;);
                    preStr = preStr.Replace(&quot; &quot;, &quot;&quot;);
                    WebClient wc = new WebClient();
                    wc.DownloadFile(imgurlAry[i], HttpContext.Current.Server.MapPath(path) + &quot;/&quot; + preStr + imgurlAry[i].Substring(imgurlAry[i].LastIndexOf(&quot;/&quot;) + 1));
                    strHTML = strHTML.Replace(imgurlAry[i], path + preStr + imgurlAry[i].Substring(imgurlAry[i].LastIndexOf(&quot;/&quot;) + 1));
                }
            }
            catch (Exception ex)
            {
                //return ex.Message;
            }
            return imgurlAry.Length;
        }
    }
}

转载于:https://www.cnblogs.com/superfang/archive/2008/07/03/1234599.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值