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

推荐订阅源

L
LangChain Blog
博客园 - 司徒正美
美团技术团队
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Troy Hunt's Blog
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
Cisco Talos Blog
Cisco Talos Blog
T
Tor Project blog
B
Blog
NISL@THU
NISL@THU
月光博客
月光博客
博客园 - 【当耐特】
AWS News Blog
AWS News Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
腾讯CDC
L
Lohrmann on Cybersecurity
The Cloudflare Blog
L
LINUX DO - 最新话题
S
Security @ Cisco Blogs
S
Secure Thoughts
Spread Privacy
Spread Privacy
有赞技术团队
有赞技术团队
The Last Watchdog
The Last Watchdog
Project Zero
Project Zero
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Vercel News
Vercel News
H
Hacker News: Front Page
S
SegmentFault 最新的问题
Schneier on Security
Schneier on Security
aimingoo的专栏
aimingoo的专栏
P
Privacy & Cybersecurity Law Blog
博客园 - 三生石上(FineUI控件)
Forbes - Security
Forbes - Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
I
InfoQ
T
Tailwind CSS Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
GRAHAM CLULEY
W
WeLiveSecurity
小众软件
小众软件
Recorded Future
Recorded Future
Cyberwarzone
Cyberwarzone
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org

博客园 - miniflyfish

抓取新浪博客中的图片 blogml导入blogengine注意事项 xml repeater dataset资料备忘 将新浪博客导入到blogengine webmatrix、visualstidio2010、blogengine cs2.1的安装备忘 Adapter适配器模式(结构型模式) prototype原型(创建型模式) Factory Method 抽象工厂模式(创建型) builder生成器(创建型模式) Abstract Factory 抽象工厂(创建型模式) 代码存放样例 singleton单件(创建型模式) 面向对象设计模式与原则 ASP.NET中的XML ASP.NET的事件处理 ASP.NET WEB服务器控件的使用 AJAX简介与web2.0 web 服务
修改导入的博客图片地址链接
miniflyfish · 2011-05-14 · via 博客园 - miniflyfish

  上一篇讲了如何抓取博客中的图片到本地,而要在blogengine中正确显示这些图片,还要修改导入的博客中的图片地址.

  blogengine中的图片存放在app-data下的files中,而图片的存放规律是按照日期放在相应的文件夹中,比如现在是2011年5月,这个月份发表的博客中的图片就存放在files下的2011/5/文件夹下.

  了解了这个规律,我们就可以构造图片的存放目录,并将图片链接改为图片存放地址.

  通过前面的xml文件的操作,我们可以读出每篇博客的发表日期

datePublished = Convert.ToDateTime(xmldoc.GetElementsByTagName("post_date")[i].ChildNodes[0].Value.ToString());

这个日期的格式是xxxx-xx-xx,我们只要取得年份和月份就可以了

postyear = xmldoc.GetElementsByTagName("post_date")[i].ChildNodes[0].Value.ToString().Substring(04);
postmonth 
= xmldoc.GetElementsByTagName("post_date")[i].ChildNodes[0].Value.ToString().Substring(52);
 
if (postmonth.Substring(01== "0")
    {
        postmonth 
= postmonth.Substring(11);
    }

这样我们就可以使用获得的postyear和postmonth构造图片的存放目录

 imgfolder="images\\"+postyear+"\\"+postmonth;

注:示例中为了测试暂时将图片存放于images文件夹下

抓取图片后存放位置就是

FileStream writer = new FileStream(MapPath(imgfolder)+ "\\" + filename + "_s.jpg", FileMode.OpenOrCreate, FileAccess.Write);

代码中_s用于区分小图片与原始图片文件名

接下来还有一个重要的步骤就是替换图片地址及链接地址,这里要用到Regex.Replace这个方法

 MatchCollection mc = Regex.Matches(content, @"<a\s+href=""http://(?<website>.+?)/showpic.html#url=(?<url>.+?)""\s+target=""_blank"">(?<content>.+?)</a>");
  
foreach (Match m in mc)
    {
      
   replacestring = "<a href='/image.axd?picture=" + postyear + "%2f" + postmonth + "%2f" + filename + ".jpg'><img src='/image.axd?picture=" + postyear + "%2f" + postmonth + "%2f" + filename + "_s.jpg'></a>";
        

         content 
= Regex.Replace(content, m.Value, replacestring);

  }

 这里的技巧点可参考文档:

Regex.Replace的简单使用

http://kb.cnblogs.com/a/1241770/

关于Regex.Replace只替换第一个的问题

http://hi.baidu.com/lovebeast/blog/item/57ba0995a04f36067bf4809b.html

javascript正则表达式中使用变量关键字

http://www.cnblogs.com/symbol441/archive/2007/12/21/1008704.html

ASP中轻松实现变量名-值变换
http://www.syscy.com/articleview/2009-3-26/article_view_712.htm