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

推荐订阅源

MyScale Blog
MyScale Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
博客园 - 叶小钗
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
雷峰网
雷峰网
宝玉的分享
宝玉的分享
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
爱范儿
爱范儿
小众软件
小众软件
K
Kaspersky official blog
P
Proofpoint News Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - Franky
V
Vulnerabilities – Threatpost
博客园_首页
Microsoft Security Blog
Microsoft Security Blog
C
Cybersecurity and Infrastructure Security Agency CISA
V
V2EX
C
Check Point Blog
S
Schneier on Security
P
Palo Alto Networks Blog
IT之家
IT之家
GbyAI
GbyAI
T
Threat Research - Cisco Blogs
Hugging Face - Blog
Hugging Face - Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Apple Machine Learning Research
Apple Machine Learning Research
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tailwind CSS Blog
Project Zero
Project Zero
Y
Y Combinator Blog
V
Visual Studio Blog
Simon Willison's Weblog
Simon Willison's Weblog
T
Threatpost
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
S
Securelist
C
CERT Recently Published Vulnerability Notes
A
Arctic Wolf
M
MIT News - Artificial intelligence
人人都是产品经理
人人都是产品经理

博客园 - 石川

moodle更改记录 新校区效果图 大鹏出品,必属精品 ubuntu设置 2009年2月份3月份工作安排 SHOW下房间 book hmidea new eTable “E表”策划书 《绐老师的建议》读后感 高中教学水平评估宣传片 随想 笔记本 大学作品回顾 清华大学统一用户认证及用户漫游系统ASP.NET版 毕业一个月 GridView控件修改、删除、分页、排序示例(修改含有DropDownList控件)
读取yahoo的天气,并下载其中的图片和FLASH到本地
石川 · 2007-08-01 · via 博客园 - 石川

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Text;

/// <summary>
/// weather 的摘要说明
/// </summary>

public class weather
{
    
private string htmlInfo;
    
public weather(string city)
    
{
        
//
        
// TODO: 在此处添加构造函数逻辑
        
//
        string fileName = DateTime.Now.ToString("yyyyMMdd"+ city + ".txt";  //文件名
        string FilePath = System.Web.HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath) + "\\images\\";
        fileName 
= FilePath + fileName;
        
string html = "";
        Encoding code 
= Encoding.GetEncoding("utf-8");
        
int start, stop;
        
if (!File.Exists(FilePath + fileName))
        
{

            WebRequest wreq 
= WebRequest.Create("http://weather.cn.yahoo.com/weather.html?city=" + city);

            HttpWebResponse wresp 
= (HttpWebResponse)wreq.GetResponse();
            Stream s 
= wresp.GetResponseStream();

            StreamReader objreader 
= new StreamReader(s);

            
string sline = "";
            
int i = 0;

            
while (sline != null)
            
{
                i
++;
                sline 
= objreader.ReadLine();
                
if (sline != null)
                    html 
+= sline;
            }


            start 
= html.IndexOf("<!--today -->");
            stop 
= html.LastIndexOf("<!--//today -->");
            html 
= html.Substring(start, stop - start);

            html 
= RequestFileFromHtml(html, "images/", FilePath);

            
//保存到Text文件中
            StreamWriter sw = null;
            
try
            
{
                sw 
= new StreamWriter(fileName, false, code);
                sw.Write(html);
                sw.Flush();
            }

            
catch (Exception ex)
            
{
                
throw ex;
            }

            
finally
            
{
                sw.Close();
            }

        }

        
else
        
{
            
//读取文件
            StreamReader sr = null;

            
//读取 
            try
            
{
                sr 
= new StreamReader(fileName, code);
                html 
= sr.ReadToEnd();
            }

            
catch (Exception ex)
            
{
                
throw ex;
            }

            
finally
            
{
                sr.Close();
            }

        }

        
this.htmlInfo = html;
    }


    
/// <summary>
    
/// 读取今天的天气
    
/// </summary>
    
/// <returns></returns>

    public string getToday()
    
{
        
string html = this.htmlInfo;
        
int start = html.IndexOf("<!--today -->");
        
int stop = html.IndexOf("<!--//today -->");
        
string html1 = html.Substring(start, stop - start);

        start 
= html1.IndexOf("<span class=\"ft1\">");
        stop 
= html1.LastIndexOf("</span>");

        
string str = "";
        
if (stop > start)
        
{
            str 
= html1.Substring(start, stop - start);
        }


        
return str;
    }


    
/// <summary>
    
/// 读取三天的天气
    
/// </summary>
    
/// <returns></returns>

    public string getThreeDay()
    
{
        
string html = this.htmlInfo;
        html 
= html.Replace("<!--//today -->""&");
        
string[] a = html.Split('&');
        
string str = "";
        str 
+= "<table width=100% border=0 align=center cellpadding=4 cellspacing=0>";
        str 
+= "<tr>";
        
for (int i = 0; i < a.Length; i++)
        
{
            
if (a[i] != "")
            
{
                str 
+= " <td width=33%>" + a[i] + "</td>";
            }

        }

        str 
+= "</tr>";
        str 
+= "</table>";
        
return str;
    }


    
/// <summary>
    
/// 下载图片和SWF并修改链接
    
/// </summary>
    
/// <param name="html">要转换的字符串</param>
    
/// <param name="fileUrl">链接</param>
    
/// <param name="filePath">保存路径</param>
    
/// <returns></returns>

    private string RequestFileFromHtml(string html, string fileUrl, string filePath)
    
{
        Uri url;
        
string fileName;
        WebClient c 
= new WebClient();
        
string p = @"((http|https|ftp):(\/\/|\\\\){1}(([A-Za-z0-9_-])+[.]){1,}(net|com|cn|org|cc|tv|[0-9]{1,3})(\S*\/)((\S)+[.]{1}(gif|jpg|png|swf)))";
        Regex r 
= new Regex(p, RegexOptions.Compiled | RegexOptions.IgnoreCase);
        MatchCollection mc 
= r.Matches(html);

        
if (mc.Count > 0)
        
{
            List
<Uri> urlList = new List<Uri>();
            
int matchIndex = 0;

            
for (int i = 0; i < mc.Count; i++)
            
{
                url 
= new Uri(mc[i].Value);

                
bool repeated = false;
                
for (int j = 0; j < urlList.Count; j++)
                
{
                    
if (url == urlList[j])
                    
{
                        repeated 
= true;
                        
break;
                    }

                }


                
if (!repeated)
                
{
                    urlList.Add(url);
                    matchIndex
++;
                }

            }


            
for (int i = 0; i < urlList.Count; i++)
            
{
                url 
= urlList[i];
                
//fileExt = url.AbsoluteUri.Substring(url.AbsoluteUri.LastIndexOf("."));
                
//fileName = string.Format("{0:yyMMddHHmmssff}{1}{2}", DateTime.Now, i, fileExt);
                fileName = url.AbsoluteUri.Substring(url.AbsoluteUri.LastIndexOf("/"+ 1);

                html 
= html.Replace(url.AbsoluteUri, fileUrl + fileName);
                
if (!File.Exists(filePath + fileName))
                
{
                    
try
                    
{
                        c.DownloadFile(url, filePath 
+ fileName);
                    }

                    
catch
                    
{

                    }

                }

            }

        }


        
return html;
    }
 


}