通过IP查询城市天气预报(抓取sina网天气预报)
2010-03-26 13:37
471 查看
今天的一个项目需要使用到天气预报这个小功能,这两天找了许多,都是第三方的,但是经理说被360拦截了
没办法了 只有自己写了。
下面是到sina网上抓取的代码
我用纯真IP数据库 匹配地址
城市信息是用xml保存的。
没办法了 只有自己写了。
下面是到sina网上抓取的代码
public string GetWeather(string city) { string weacherhtml = string.Empty; //转换输入参数的编码类型 string mycity = System.Web.HttpUtility.UrlEncode(city, System.Text.UnicodeEncoding.GetEncoding("GB2312")); //初始化新的 WebRequest HttpWebRequest webrt = (HttpWebRequest)WebRequest.Create("http://php.weather.sina.com.cn/search.php?city=" + mycity); HttpWebResponse webrs = (HttpWebResponse)webrt.GetResponse(); //从Internet资源返回数据流 Stream stream = webrs.GetResponseStream(); //读取数据流 StreamReader srm = new StreamReader(stream, System.Text.Encoding.Default); //读取数据 weacherhtml = srm.ReadToEnd(); srm.Close(); stream.Close(); webrs.Close(); //针对不同的网站,请查看HTML源文件 int start = weacherhtml.IndexOf("<!-- box begin-->"); //取的字符位置 int end = weacherhtml.IndexOf("<!-- box end-->"); return weacherhtml.Substring(start + 17, end - start); }
我用纯真IP数据库 匹配地址
城市信息是用xml保存的。
相关文章推荐
- 想在自己的android应用中获得当天的天气情况,这该怎么做呢?不用担心。中国国家气象局提供了获取所在城市天气预报信息接口。通过这个接口,我们就可以获取天气信息了。
- 城市天气三小时预报,天气预报接口实现
- 聚合数据全国天气预报--ajax 通过城市名取数据
- 通过经纬度获取城市yahoo天气预报(最新2013-4-17)
- 聚合数据全国天气预报API--ajax 通过城市名取数据
- Ajax 通过城市名获取数据(全国天气预报API)
- 天气预报插件代码--通过传递城市拼音名,最简单的天气插件--更多看2345网站
- 通过IP查询客户端用户所在地的天气预报
- 使用正则表达式和REPLACE,SPLIT获得7,8,9天单个城市天气情况抓取解析代码
- Web服务入门之二:通过axis2调用远程Web Service,实现天气预报功能
- 天气预报--保存已查询城市,历史记录功能实现
- R语言rvest包3步爬取中国天气网国内所有城市(县)7天实时天气预报数据
- 利用中国天气预报接口实现简单天气预报
- ts实例应用之通过城市的citycode获取天气数据(逻辑)
- 天气预报之抓取、解析、存入MYSQL数据库模块实现,不规范(代码)
- 天气预报--查询其他城市天气功能实现
- 天气预报--保存已查询城市,历史记录功能实现
- Android通过调用Webservice实现天气预报
- 天气预报接口,在自己的网页上加入天气预报吧
- python正则表达式分析新浪网天气预报,通过pyfetion发送短信的代码