马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

C#通过抓取页面获取当天天气信息

[复制链接]
查看3173 | 回复3 | 2017-4-18 09:44:42 | 显示全部楼层 |阅读模式
[C#] 纯文本查看 复制代码
/// <summary>  
///  得到天气数据  
/// </summary>  
/// <returns>数组(0、天气;1、气温;2、风力;3、紫外线;4、空气)</returns>  
public static string[] GetWeather()  
{  
    Regex regex;  
  
    string[] weather = new string[5];  
    string content = "";  
  
    Match mcTmp;  
    Match mcCity;  
    int k = 1;  
  
    HttpWebResponse theResponse;  
    WebRequest theRequest;  
  
  
    //ss1.htm 注意:ss1-ss303代表了不同的城市,是不连续的  
    theRequest = WebRequest.Create("http://weather.news.qq.com/inc/ss1.htm");  
    try  
    {  
        theResponse = (HttpWebResponse)theRequest.GetResponse();  
  
        using (System.IO.Stream sm = theResponse.GetResponseStream())  
        {  
            System.IO.StreamReader read = new System.IO.StreamReader(sm, Encoding.Default);  
            content = read.ReadToEnd();  
        }  
    }  
    catch (Exception)  
    {  
        content = "";  
    }  
  
  
    string parttenTmp = "<td height=\"23\" width=\"117\" background=\"/images/r_tembg5.gif\" align=\"center\">(?<item1>[^<]+)</td>";  
    k = 1;  
    regex = new Regex(parttenTmp, RegexOptions.Compiled | RegexOptions.IgnoreCase);  
    for (mcTmp = regex.Match(content), k = 1; mcTmp.Success; mcTmp = mcTmp.NextMatch(), k++)  
    {  
  
        weather[0] = mcTmp.Groups["item1"].Value;  
    }  
    parttenTmp = "height=\"23\" align=\"center\">(?<item1>[^/]+)</td>";  
    k = 1;  
    regex = new Regex(parttenTmp, RegexOptions.Compiled | RegexOptions.IgnoreCase);  
    for (mcTmp = regex.Match(content), k = 1; mcTmp.Success; mcTmp = mcTmp.NextMatch(), k++)  
    {  
        weather[k] = mcTmp.Groups["item1"].Value;  
    }  
    return weather;  
}  

C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
ibcadmin | 2017-4-18 09:45:16 | 显示全部楼层
小叶子
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
Amy尾巴 | 2017-4-18 09:50:59 | 显示全部楼层
666
meng | 2017-4-18 10:00:34 | 显示全部楼层
学到了
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则