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

推荐订阅源

Cyberwarzone
Cyberwarzone
Vercel News
Vercel News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
aimingoo的专栏
aimingoo的专栏
B
Blog RSS Feed
A
About on SuperTechFans
T
The Blog of Author Tim Ferriss
爱范儿
爱范儿
腾讯CDC
S
SegmentFault 最新的问题
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The Hacker News
The Hacker News
J
Java Code Geeks
大猫的无限游戏
大猫的无限游戏
B
Blog
IT之家
IT之家
Spread Privacy
Spread Privacy
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
C
Cisco Blogs
Recent Announcements
Recent Announcements
H
Hacker News: Front Page
AI
AI
I
InfoQ
H
Heimdal Security Blog
T
Threatpost
Cisco Talos Blog
Cisco Talos Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
I
Intezer
W
WeLiveSecurity
SecWiki News
SecWiki News
MongoDB | Blog
MongoDB | Blog
宝玉的分享
宝玉的分享
博客园 - 【当耐特】
云风的 BLOG
云风的 BLOG
T
Threat Research - Cisco Blogs
V2EX - 技术
V2EX - 技术
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
O
OpenAI News
阮一峰的网络日志
阮一峰的网络日志
T
Troy Hunt's Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
雷峰网
雷峰网
T
Tor Project blog
有赞技术团队
有赞技术团队
Schneier on Security
Schneier on Security
Last Week in AI
Last Week in AI

博客园 - 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;
        }
    }
}