如何用js调用天气预报代码 最新

JavaScript015

如何用js调用天气预报代码 最新,第1张

直接使用插件,如

<iframe width="214" scrolling="no" height="54" frameborder="0" allowtransparency="true" src="http://i.tianqi.com/index.php?c=code&id=42&icon=1&num=3"></iframe>

效果如下

这个插件来源是http://www.tianqi.com/plugin/

//这是我以前开发天气wedget的时候写过的代码,原理是连接到yahoo api(返回XML),先用cityCode查国家 然后用woeid查地方,这是测试时写过的代码。Yahoo 天气api 好像一部分收费 最后没用yahoo 用 weatherbug的api 那个代码忘了存在哪里了,如果 非常着急的话在跟我说吧 我给你找找,还有调用api的原理,一般不会直接调用api 因为每个人访问时都调用一次的话系统受不了。最好写个windows service 每个一段时间调用一次api然后以.xml形式存放到一个文件夹,在系统中只调用xml文件就好了。如果api一时访问不到了也不会出问题。

才看到你想要的是前台代码, 这是后台的

private string GetWeather(string cityCode)

{

string weather = string.Empty

if (cityCode == "" || string.IsNullOrEmpty(cityCode)) { cityCode = "seoul"}

XmlDocument document1 = new XmlDocument()

document1.Load("http://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text=" + cityCode + "&format=xml")

XmlNodeList nodes1 = document1.GetElementsByTagName("woeid")

string strWoeid = nodes1[0].InnerText

XmlDocument document = new XmlDocument()

document.Load("http://weather.yahooapis.com/forecastrss?w=" + strWoeid + "&u=c")

XmlNodeList nodes = document.GetElementsByTagName("forecast",

@"http://xml.weather.yahoo.com/ns/rss/1.0")

foreach (XmlNode node in nodes)

{

Console.WriteLine("日期:{0},星期:{1},天气:{2},温度:{3}°C 至 {4}°C",

node.Attributes["date"].InnerText,

node.Attributes["day"].InnerText,

node.Attributes["text"].InnerText,

node.Attributes["low"].InnerText,

node.Attributes["high"].InnerText)

//FToC(int.Parse(node.Attributes["low"].InnerText)),

// FToC(int.Parse(node.Attributes["high"].InnerText)))

// //woeid

// // http://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text="Wonju"&format=xml

}

return weather

}

//给你一个网址

//http://m.weather.com.cn/data/101010100.html

//http://blog.csdn.net/hello_haozi/article/details/7564223

NSString *shangHai = @"http://m.weather.com.cn/data/101020100.html"

NSOperationQueue *queue = [[NSOperationQueue alloc] init]

NSURL *url1 = [NSURL URLWithString:shangHai]

NSURLRequest *request1 = [[NSURLRequest alloc] initWithURL:url1]

[NSURLConnection sendAsynchronousRequest:request1 queue:queue completionHandler:^(NSURLResponse *response, NSData *da, NSError *error) {

if (da) {

NSDictionary *shangHaiDict = [NSJSONSerialization JSONObjectWithData:da options:NSJSONReadingMutableLeaves error:&error]

NSLog(@"%@",shangHaiDict)

}

}]

这是上海的天气 ,不知道是否回答了你的问题