读取yahoo的天气,并下载其中的图片和FLASH到本地

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;
    }
 


}

posted on 2007-08-01 20:49  石川  阅读(640)  评论(0编辑  收藏  举报