惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
Google DeepMind News
Google DeepMind News
aimingoo的专栏
aimingoo的专栏
Recent Announcements
Recent Announcements
A
About on SuperTechFans
U
Unit 42
MyScale Blog
MyScale Blog
J
Java Code Geeks
博客园_首页
Blog — PlanetScale
Blog — PlanetScale
D
Docker
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 司徒正美
量子位
月光博客
月光博客
G
Google Developers Blog
V
V2EX
博客园 - 聂微东
宝玉的分享
宝玉的分享
IT之家
IT之家
Vercel News
Vercel News

博客园 - roboth

转:“由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值”的解决方法 .net tostring format格式说明 - roboth 一、DIP简介(DIP--Dependency Inversion Principle): 请确认 &lt;Import&gt; 声明中的路径正确,且磁盘上存在该文件。 异步委托 Visual Studio 最常用的13个快捷键 H1N1:速饮配方 通过注册表获取文件的ContentType [转]解决ASP.NET Web Applicatio超时时间已到.在操作完成之前超时时间已过或服务器未响应 - roboth 在线创建pdf msdn之sp_xml_preparedocument Route命令使用详解 电子书下载 买药记 xslt之<xsl:apply-templates - roboth - 博客园 盲人摸象 sql产生日历表 dom操作时机 js小技巧之访问元素属性
抓取图片
roboth · 2009-05-21 · via 博客园 - roboth


using System;
using System.Collections.Generic;
using System.Linq;
using System.
Text;
using System.Net;
using System.
Text.RegularExpressions;
using System.IO;
namespace ImgSnatch
{
    class Program
    {
        static void Main(string
[] args)
        {
            Pic_Remote("
<img src=http://www.google.cn/images/nav_logo4.png>");
        }
        private static string Pic_Remote(string news_Content)
        {
            string htmlStr 
= news_Content;
            string nowyymm 
= DateTime.Now.ToString("yyyy-MM");    //当前年月
            string nowdd 
= DateTime.Now.ToString("dd"); //当天号数
            string path
=string.Format(@"d:\imgSnatch\images\{0}\{1}",nowyymm,nowdd);
            Directory.CreateDirectory(path);
            string returnValue 
= "";
            returnValue 
= SaveUrlPics(htmlStr, path, nowyymm, nowdd);
            
return returnValue;
        }
        
//下载图片到本地
        
public static string SaveUrlPics(string strHTML, string path, string nowyymm, string nowdd)
        {
            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], path + "/+ preStr + imgurlAry[i].Substring(imgurlAry[i].LastIndexOf("/") + 1));
                    
//替换原图片地址
                    string imgPath 
= "/Files/Remoteupfile/+ nowyymm + "/+ nowdd;
                    strHTML 
= strHTML.Replace(imgurlAry[i], imgPath + "/+ preStr + imgurlAry[i].Substring(imgurlAry[i].LastIndexOf("/") + 1));
                }
            }
            catch (Exception ex)
            {
                
//return ex.Message;
            }
            
return strHTML;
        }
//获取图片标志
        private static  string
[] GetImgTag(string htmlStr)
        {
            Regex regObj 
= new Regex("<img.+?>", 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;
        }
        
//获取图片URL地址
        private static  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;
        }
    }
}