为什么这个Yahoo天气API查询不起作用?

时间:2015-07-02 13:11:38

标签: api yahoo weather

从渥太华获取预测的查询不起作用:

SELECT * FROM weather.forecast WHERE woeid IN (SELECT woeid FROM geo.places WHERE text="Ottawa, Canada" limit 1)

结果:

"query": {
  "count": 1,
  "created": "2015-07-02T13:06:45Z",
  "lang": "en-US",
  "results": {
   "channel": {
    "title": "Yahoo! Weather - Error",
    "description": "Yahoo! Weather Error",
    "item": {
     "title": "City not found",
     "description": "\nInvalid Input /forecastrss?w=91982014\n"
    }
   }
  }
 }

自己执行,子查询得到正确的结果:

SELECT woeid FROM geo.places WHERE text="Ottawa, Canada" limit 1

结果:

"query": {
  "count": 1,
  "created": "2015-07-02T13:00:47Z",
  "lang": "en-US",
  "results": {
   "place": {
    "woeid": "91982014"
   }
  }
 }

这适用于我尝试的所有其他城市,当我忽略“限制1”时,它也适用于渥太华,但返回的结果太多。我做错了还是API有问题?

1 个答案:

答案 0 :(得分:0)

我认为问题在于,对于渥太华,有多个结果(多个WOEID),并且并非所有这些WOEID在weather.forecast表中都有相应的条目。我认为 是因为其中一些条目的级别不是我们预测的水平。

查询SELECT woeid FROM geo.places WHERE text="Ottawa, Canada"会产生以下结果:

"results": {
 "place": [
  {
   "woeid": "91982014"
  },
  {
   "woeid": "29375164"
  },
  {
   "woeid": "12483153"
  }
 ]
}

如果您修改查询以包含地点类型SELECT woeid, placeTypeName.content FROM geo.places WHERE text="Ottawa, Canada",则可以看到结果包含城镇,县/区和岛屿,而城镇是结果中的第一项:

"results": {
 "place": [
  {
   "woeid": "91982014",
   "placeTypeName": "Town"
  },
  {
   "woeid": "29375164",
   "placeTypeName": "County/District"
  },
  {
   "woeid": "12483153",
   "placeTypeName": "Island"
  }
 ]
}

在返回过多结果SELECT * FROM weather.forecast WHERE woeid IN (SELECT woeid FROM geo.places WHERE text="Ottawa, Canada")的完整查询的结果中,您可以看到名为diagnostics的部分,其中包含一个名为url的数组,该数组提供的天气API网址因查询而调用:

http://weather.yahooapis.com/forecastrss?w=12483153
http://weather.yahooapis.com/forecastrss?w=29375164
http://weather.yahooapis.com/forecastrss?w=91982014

并且91982014的结果是错误:

您可以从结果SELECT * FROM weather.forecast WHERE woeid IN (SELECT woeid FROM geo.places WHERE text="Ottawa, Canada") AND title NOT LIKE "%Error%"中删除错误案例,但仍会提供两个结果。

看起来你真正想要做的就是意识到加拿大有三个地方"渥太华"名称:安大略省的首府渥太华,安大略省的渥太华县/区,以及努勒维特境内的渥太华群岛。因此,您需要更具体地查询您的查询。

SELECT * FROM weather.forecast WHERE woeid IN (SELECT woeid FROM geo.places WHERE text="Ottawa, Ontario, Canada") AND title NOT LIKE "%Error%"

感觉很好,但它只能产生一个结果。