-1

私が取り組んでいる Skype ボットには Skype4Py を使用しています。
API が応答するものをどのように注文できるのか疑問に思っていました。
たとえば、天気を取得したい場合は、!weather
と入力すると、次のように応答します。

気象情報を収集しています。お待ちください...
天気: { "データ": { "現在の状態": [ {"雲量": "0", "湿度": "39", "観測時間": "11:33 AM", "precipMM": "0.0", "気圧": "1023", "temp_C": "11", "temp_F": "51", "visibility": "16", "weatherCode": "113", "weatherDesc": [ { "値": "クリア" } ], "weatherIconUrl": [ {"値": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0008_clear_sky_night.png" } ], "winddir16Point": "N", "winddirDegree": "0", "windspeedKmph": "0", "

そして、私はそれをもっと好きにしたいと思います:

天気:
現在の気温: 51 F | 22 C
湿度: 39%
風速: 0 MPH

または、そのようにきれいに注文されたもの。
こうすることで、Skype での見栄えが良くなり、よりプロフェッショナルに見えます。


コードを追加する必要がありました:

    def weather(zip):
try:
    return urllib2.urlopen('http://api.worldweatheronline.com/free/v1/weather.ashx?q='+zip+'&format=json&num_of_days=1&fx=no&cc=yes&key=r8nkqkdsrgskdqa9spp8s4hx' ).read()
except:
    return False

それが私の functions.py です

これは私のcommands.pyです:

                        elif msg.startswith('!weather '):
                    debug.action('!weather command executed.')
                    send(self.nick + 'Gathering weather information. Please wait...')
                    zip = msg.replace('!weather ', '', 1);
                    current = functions.weather(zip)
                    if 4 > 2:
                        send('Weather: ' + current)
                    else:
                        send('Weather: ' + current)

私が述べたように、私はSkype4Pyを使用しています。

4

1 に答える 1

0

API は を返しjsonています。これを解析する必要があります。

import requests

url = 'http://api.worldweatheronline.com/free/v1/weather.ashx'
params = {'format': 'json',
          'num_of_days': 1, 'fx': 'no', 'cc': 'yes', 'key': 'sekret'}
params['zip'] = 90210

r = requests.get(url, params=params)
if r.status_code == 200:
   results = r.json()

print('Weather:
Current Temp: {0[temp_f]} F | {0[temp_c]} C
Humidity: {0[humidity]}%
Wind Speed: {0[windspeedMiles]} MPH'.format(results[0]))

私は優れたrequestsライブラリを使用しています

于 2013-12-15T12:23:53.527 に答える