DiscordにPythonで天気予報を流したい
Discordの発言を監視し、 /tenki [都道府県]
というコマンドを受け付けて天気予報を投稿するbotを作成したが、下記のようなエラーが発生した。
Error:
File "C:\Users\USER\AppData\Local\Programs\Python\Python35\lib\site- packages\requests\models.py", line 892, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\USER\AppData\Local\Programs\Python\Python35\lib\json\__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "C:\Users\USER\AppData\Local\Programs\Python\Python35\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File
"C:\Users\USER\AppData\Local\Programs\Python\Python35\lib\json\decoder.py", line
357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Code:
import discord
import json
import requests
f = open("tenki.json", encoding='utf-8_sig')
tenki = json.load(f)
f.close()
client = discord.Client()
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
@client.event
async def on_message(message):
if message.content.startswith('/tenki'):
url = 'http://weather.livedoor.com/forecast/webservice/json/v1'
payload = {'city': ["tenki"]}
data = requests.get(url, params = payload).json()
await client.send_message(message.channel, data['title'])
await client.send_message(message.channel, data["link"])
client.run(token)
json code:
{"道北": "011000","沖縄県": "471010"}