RegEx captures the Sina weather forecast data !!!

Source: Internet
Author: User

/// <Summary>
/// One-day weather forecast for Sina
/// </Summary>
/// <Returns> </returns>
Public static xmldatadocument getsinaweather ()
{
// Http://weather.news.sina.com.cn/images/figureWeather/map/northEast.html
// Http://weather.news.sina.com.cn/images/figureWeather/map/eastOfChina.html
// Http://weather.news.sina.com.cn/images/figureWeather/map/northOfChina.html
// Http://weather.news.sina.com.cn/images/figureWeather/map/southOfChina.html
// Http://weather.news.sina.com.cn/images/figureWeather/map/southWest.html
// Http://weather.news.sina.com.cn/images/figureWeather/map/northWest.html

Xmldatadocument objxml = new xmldatadocument ();
Objxml. loadxml ("<root/> ");
// Northeast China
String content = getcontent ("http://weather.news.sina.com.cn/images/figureWeather/map/northEast.html", "gb2312 ");

// After capturing the content, start to analyze the data
RegEx;
Match MC;
Xmlelement objxmlcitylist = objxml. createelement ("citylist ");

String partten = "city :(? <City> [^ <] +) <br> weather :(? <Weather> [^ <] +) <br> temperature :(? <Temperature> [^ <] +) <br> wind direction :(? <Windway> [^ <] +) <br> Wind Power :(? <Wind> [^ <] +) <br> ";
RegEx = new RegEx (partten, regexoptions. Compiled | regexoptions. ignorecase );
Objxmlcitylist. setattribute ("vdatetime", datetime. Now. tow.datestring ());
// City: Harbin <br> weather: Cloudy to clear <br> temperature:-10 ℃ ~ -1 ℃ <br> wind direction: West Wind <br> Wind Power: less than 3 <br>
Int I = 1;
For (MC = RegEx. Match (content), I = 1; Mc. success; MC = mc. nextmatch (), I ++)
{
Try
{
Yesun. edzh. BLL. log. loghelper. writeerror (I + "," + MC. Groups ["city"]. value. Trim ());
Xmlelement objxmlelementcity = objxml. createelement ("city ");
Objxmlelementcity. setattribute ("orders", "1 ");
Objxmlelementcity. setattribute ("city", MC. Groups ["city"]. value. Trim ());
Objxmlelementcity. setattribute ("Weather", MC. Groups ["Weather"]. value. Trim ());
Objxmlelementcity. setattribute ("temperature", MC. Groups ["temperature"]. value. Trim ());
Objxmlelementcity. setattribute ("windway", MC. Groups ["windway"]. value. Trim ());
Objxmlelementcity. setattribute ("wind", MC. Groups ["wind"]. value. Trim ());
Objxmlcitylist. appendchild (objxmlelementcity );
}
Catch (exception ex)
{
Yesun. edzh. BLL. log. loghelper. writeerror (ex. Message );
}
}

// East
Content = getcontent ("http://weather.news.sina.com.cn/images/figureWeather/map/eastOfChina.html", "gb2312 ");
For (MC = RegEx. Match (content), I = 1; Mc. success; MC = mc. nextmatch (), I ++)
{
Try
{
Xmlelement objxmlelementcity = objxml. createelement ("city ");
Objxmlelementcity. setattribute ("orders", "2 ");
Objxmlelementcity. setattribute ("city", MC. Groups ["city"]. value. Trim ());
Objxmlelementcity. setattribute ("Weather", MC. Groups ["Weather"]. value. Trim ());
Objxmlelementcity. setattribute ("temperature", MC. Groups ["temperature"]. value. Trim ());
Objxmlelementcity. setattribute ("windway", MC. Groups ["windway"]. value. Trim ());
Objxmlelementcity. setattribute ("wind", MC. Groups ["wind"]. value. Trim ());
Objxmlcitylist. appendchild (objxmlelementcity );
}
Catch (exception ex)
{
Yesun. edzh. BLL. log. loghelper. writeerror (ex. Message );
}
}
// North
Content = getcontent ("http://weather.news.sina.com.cn/images/figureWeather/map/northOfChina.html", "gb2312 ");
For (MC = RegEx. Match (content), I = 1; Mc. success; MC = mc. nextmatch (), I ++)
{
Try
{
Yesun. edzh. BLL. log. loghelper. writeerror (I + "," + MC. Groups ["city"]. value. Trim ());
Xmlelement objxmlelementcity = objxml. createelement ("city ");
Objxmlelementcity. setattribute ("orders", "3 ");
Objxmlelementcity. setattribute ("city", MC. Groups ["city"]. value. Trim ());
Objxmlelementcity. setattribute ("Weather", MC. Groups ["Weather"]. value. Trim ());
Objxmlelementcity. setattribute ("temperature", MC. Groups ["temperature"]. value. Trim ());
Objxmlelementcity. setattribute ("windway", MC. Groups ["windway"]. value. Trim ());
Objxmlelementcity. setattribute ("wind", MC. Groups ["wind"]. value. Trim ());
Objxmlcitylist. appendchild (objxmlelementcity );
}
Catch (exception ex)
{
Yesun. edzh. BLL. log. loghelper. writeerror (ex. Message );
}
}

// South
Content = getcontent ("http://weather.news.sina.com.cn/images/figureWeather/map/southOfChina.html", "gb2312 ");
For (MC = RegEx. Match (content), I = 1; Mc. success; MC = mc. nextmatch (), I ++)
{
Try
{
Xmlelement objxmlelementcity = objxml. createelement ("city ");
Objxmlelementcity. setattribute ("orders", "4 ");
Objxmlelementcity. setattribute ("city", MC. Groups ["city"]. value. Trim ());
Objxmlelementcity. setattribute ("Weather", MC. Groups ["Weather"]. value. Trim ());
Objxmlelementcity. setattribute ("temperature", MC. Groups ["temperature"]. value. Trim ());
Objxmlelementcity. setattribute ("windway", MC. Groups ["windway"]. value. Trim ());
Objxmlelementcity. setattribute ("wind", MC. Groups ["wind"]. value. Trim ());
Objxmlcitylist. appendchild (objxmlelementcity );
}
Catch (exception ex)
{
Yesun. edzh. BLL. log. loghelper. writeerror (ex. Message );
}
}

// Nanxi
Content = getcontent ("http://weather.news.sina.com.cn/images/figureWeather/map/southWest.html", "gb2312 ");
For (MC = RegEx. Match (content), I = 1; Mc. success; MC = mc. nextmatch (), I ++)
{
Try
{
Xmlelement objxmlelementcity = objxml. createelement ("city ");
Objxmlelementcity. setattribute ("orders", "5 ");
Objxmlelementcity. setattribute ("city", MC. Groups ["city"]. value. Trim ());
Objxmlelementcity. setattribute ("Weather", MC. Groups ["Weather"]. value. Trim ());
Objxmlelementcity. setattribute ("temperature", MC. Groups ["temperature"]. value. Trim ());
Objxmlelementcity. setattribute ("windway", MC. Groups ["windway"]. value. Trim ());
Objxmlelementcity. setattribute ("wind", MC. Groups ["wind"]. value. Trim ());
Objxmlcitylist. appendchild (objxmlelementcity );
}
Catch (exception ex)
{
Yesun. edzh. BLL. log. loghelper. writeerror (ex. Message );
}
}

// North West
Content = getcontent ("http://weather.news.sina.com.cn/images/figureWeather/map/northWest.html", "gb2312 ");
For (MC = RegEx. Match (content), I = 1; Mc. success; MC = mc. nextmatch (), I ++)
{
Try
{
Xmlelement objxmlelementcity = objxml. createelement ("city ");
Objxmlelementcity. setattribute ("orders", "6 ");
Objxmlelementcity. setattribute ("city", MC. Groups ["city"]. value. Trim ());
Objxmlelementcity. setattribute ("Weather", MC. Groups ["Weather"]. value. Trim ());
Objxmlelementcity. setattribute ("temperature", MC. Groups ["temperature"]. value. Trim ());
Objxmlelementcity. setattribute ("windway", MC. Groups ["windway"]. value. Trim ());
Objxmlelementcity. setattribute ("wind", MC. Groups ["wind"]. value. Trim ());
Objxmlcitylist. appendchild (objxmlelementcity );
}
Catch (exception ex)
{
Yesun. edzh. BLL. log. loghelper. writeerror (ex. Message );
}
}
Partten = "city :(? <City> [^ <] +) <br> weather :(? <Weather> [^ <] +) <br> temperature :(? <Temperature> [^ <] +) <br> Wind Power :(? <Wind> [^ <] +) <br> ";
RegEx = new RegEx (partten, regexoptions. Compiled | regexoptions. ignorecase );
// North West City: Wuhan <br> weather: light rain to overcast <br> temperature: 10 ℃ ~ 16 ℃ <br> Wind Power: less than 3 <br>
Content = getcontent ("http://weather.news.sina.com.cn/images/figureWeather/map/wholeNation.html", "gb2312 ");
For (MC = RegEx. Match (content), I = 1; Mc. success; MC = mc. nextmatch (), I ++)
{
Try
{
Xmlelement objxmlelementcity = objxml. createelement ("city ");
Objxmlelementcity. setattribute ("orders", "7 ");
Objxmlelementcity. setattribute ("city", MC. Groups ["city"]. value. Trim ());
Objxmlelementcity. setattribute ("Weather", MC. Groups ["Weather"]. value. Trim ());
Objxmlelementcity. setattribute ("temperature", MC. Groups ["temperature"]. value. Trim ());
Objxmlelementcity. setattribute ("windway ","");
Objxmlelementcity. setattribute ("wind", MC. Groups ["wind"]. value. Trim ());
Objxmlcitylist. appendchild (objxmlelementcity );
}
Catch (exception ex)
{
Yesun. edzh. BLL. log. loghelper. writeerror (ex. Message );
}
}

Objxml. documentelement. appendchild (objxmlcitylist );

Return objxml;
}

/// <Summary>
/// Page capture Interface
/// </Summary>
/// <Param name = "url"> </param>
/// <Returns> </returns>
Private Static string getcontent (string URL, string encoding)
{
String STR = "";
WebClient client = new WebClient ();
Client. headers. add ("accept", "image/GIF, image/X-xbitmap, image/JPEG, image/pjpeg, application/X-Shockwave-flash, application/vnd. MS-Excel, application/vnd. MS-PowerPoint, application/MSWord ,*/*");
Client. headers. Add ("Accept-language", "ZH-CN ");
Client. headers. Add ("UA-CPU", "x86 ");
Client. headers. add ("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; sv1 ;. net CLR 1.1.4322 ;. net CLR 2.0.50727 )");
Try
{
Byte [] buffer = client. downloaddata (URL );
If (encoding = "UTF-8 ")
{
STR = system. Text. encoding. getencoding ("UTF-8"). getstring (buffer, 0, buffer. Length );
}
Else
{
STR = system. Text. encoding. getencoding ("gb2312"). getstring (buffer, 0, buffer. Length );
}
}
Catch (exception ex)
{
Yesun. edzh. BLL. log. loghelper. writeerror (ex. Message );
}
Return STR;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.