###Pythonに関する質問です。
(DiscordのBot機能の一部です。ソースコードはTest.cog
の内容です。)
天気予報をしようとしたのですが3日分の天気を表示することがうまくできません。
どうすればいいでしょうか?
情報をjsonに直した内容はprint(resp)
で表示しています。
その際の出力内容も書いておきます。
###ソースコード
Python
1from discord.ext import commands 2import discord 3import locale 4from datetime import datetime 5from discord.utils import find 6from discord.utils import get 7import traceback 8import urllib.request 9import urllib.error 10import json 11import googletrans 12import asyncio 13import random 14import youtube_dl 15import xml.etree.ElementTree as ET 16import requests 17from lxml import etree 18from discord import Game 19from discord.ext.commands import Bot 20import aiohttp 21from html.parser import HTMLParser 22from bs4 import BeautifulSoup 23import bs4 24from pprint import pprint 25import xml.etree.ElementTree as ET 26from PIL import Image, ImageDraw, ImageFilter 27 28translator = googletrans.Translator() 29locale.setlocale(locale.LC_ALL, '') 30 31citycodes = { 32 "土浦": '080020', 33 "水戸": '080010', 34 "札幌": '016010', 35 "仙台": '040010', 36 "東京": '130010', 37 "横浜": '140010', 38 "名古屋": '230010', 39 "大阪": '270000', 40 "広島": '340010', 41 "福岡": '400010', 42 "鹿児島": '460010', 43 "那覇": '471010', 44 "神戸": '280010', 45 "さいたま": '110010' 46} 47 48class TestCog(): 49 def __init__(self, bot): 50 self.bot = bot 51 52 @commands.command() 53 async def tenki(self, ctx, options): 54 if ctx.channel.id == 557173935590998016: 55 if options in citycodes.keys(): 56 citycode = citycodes[options] 57 58 async with aiohttp.ClientSession() as session: 59 async with session.get('http://weather.livedoor.com/forecast/webservice/json/v1?city=%s'%citycode) as r: 60 resp = await r.json() 61 print(resp) 62 msg1 = resp['location']['city'] 63 msg = msg1 + "の天気は\n" 64 for f in resp['forecasts']: 65 msg += f['dateLabel'] + "が" + f['telop'] + "\n" 66 msg += "だと思うの。" 67 kuwasiku = resp['description']['text'] 68 kions = resp['temperature']['min']['celsius'] 69 kionk = resp['temperature']['max']['celsius'] 70 kosuis = resp['temperature']['min']['fahrenheit'] 71 kosuik = resp['temperature']['max']['fahrenheit'] 72 embed = discord.Embed(title="MLSBOT天気予報", description=msg, color=0x00e7ff) 73 embed.add_field(name=msg1 + "の本日の気温情報", value="気温\n最低気温\n" + kions + "\n最低気温\n" + kionk + "降水確率\n最低降水確率\n" + kosuis + "\n最高降水確率\n" + kosuik, inline=False) 74 embed.add_field(name=msg1 + "の本日の詳細", value=kuwasiku, inline=False) 75 replay = datetime.now().strftime("%Y年%m月%d日 %H時%M分%S秒") 76 embed.set_footer(text="ちなみにこれは" + replay + " 時点のものなの") 77 await ctx.send(embed=embed) 78 return 79 else: 80 embed = discord.Embed(title=options + "の天気は", description="まだわからないの・・・", color=0x00e7ff) 81 embed.add_field(name="私が天気の分かる場所は", value="・土浦\n・水戸\n・札幌\n・仙台\n・東京\n・横浜\n・名古屋\n・大阪\n・広島\n・福岡\n・鹿児島\n・那覇\n・さいたま\n・神戸\nぐらいなの", inline=False) 82 embed.add_field(name="私が追加できる都市はこの中のものだけなの", value="http://weather.livedoor.com/forecast/rss/primary_area.xml", inline=False) 83 replay = datetime.now().strftime("%Y年%m月%d日 %H時%M分%S秒") 84 embed.set_footer(text="ちなみにこれは" + replay + " 時点のものなの") 85 await ctx.send(embed=embed) 86 return 87 88def setup(bot): 89 bot.remove_command('help') 90 bot.add_cog(TestCog(bot))
###respの出力内容
json
1{'pinpointLocations': [{'link': 'http://weather.livedoor.com/area/forecast/2810000', 'name': '神戸市'}, {'link': 'http://weather.livedoor.com/area/forecast/2820100', 'name': '姫路市'}, {'link': 'http://weather.livedoor.com/area/forecast/2820200', 'name': '尼崎市'}, {'link': 'http://weather.livedoor.com/area/forecast/2820300', 'name': '明石市'}, {'link': 'http://weather.livedoor.com/area/forecast/2820400', 'name': '西宮市'}, {'link': 'http://weather.livedoor.com/area/forecast/2820500', 'name': '洲本市'}, {'link': 'http://weather.livedoor.com/area/forecast/2820600', 'name': '芦屋市'}, {'link': 'http://weather.livedoor.com/area/forecast/2820700', 'name': '伊丹市'}, {'link': 'http://weather.livedoor.com/area/forecast/2820800', 'name': '相生市'}, {'link': 'http://weather.livedoor.com/area/forecast/2821000', 'name': '加古川市'}, {'link': 'http://weather.livedoor.com/area/forecast/2821200', 'name': '赤穂市'}, {'link': 'http://weather.livedoor.com/area/forecast/2821300', 'name': '西脇市'}, {'link': 'http://weather.livedoor.com/area/forecast/2821400', 'name': '宝塚市'}, {'link': 'http://weather.livedoor.com/area/forecast/2821500', 'name': '三木市'}, {'link': 'http://weather.livedoor.com/area/forecast/2821600', 'name': '高砂市'}, {'link': 'http://weather.livedoor.com/area/forecast/2821700', 'name': '川西市'}, {'link': 'http://weather.livedoor.com/area/forecast/2821800', 'name': '小野市'}, {'link': 'http://weather.livedoor.com/area/forecast/2821900', 'name': '三田市'}, {'link': 'http://weather.livedoor.com/area/forecast/2822000', 'name': '加西市 2'}, {'link': 'http://weather.livedoor.com/area/forecast/2822100', 'name': '篠山市'}, {'link': 'http://weather.livedoor.com/area/forecast/2822300', 'name': '丹波市'}, {'link': 'http://weather.livedoor.com/area/forecast/2822400', 'name': '南あわじ市'}, {'link': 'http://weather.livedoor.com/area/forecast/2822600', 'name': '淡路市'}, {'link': 'http://weather.livedoor.com/area/forecast/2822700', 'name': '宍粟市'}, {'link': 'http://weather.livedoor.com/area/forecast/2822800', 'name': '加東市'}, {'link': 'http://weather.livedoor.com/area/forecast/2822900', 'name': 'たつの市'}, {'link': 'http://weather.livedoor.com/area/forecast/2830100', 'name': '猪名川町'}, {'link': 'http://weather.livedoor.com/area/forecast/2836500', 'name': '多可町'}, {'link': 'http://weather.livedoor.com/area/forecast/2838100', 'name': '稲美町'}, {'link': 'http://weather.livedoor.com/area/forecast/2838200', 'name': '播磨町'}, {'link': 'http://weather.livedoor.com/area/forecast/2844200', 'name': '市川町'}, {'link': 'http://weather.livedoor.com/area/forecast/2844300', 'name': '福崎町'}, {'link': 'http://weather.livedoor.com/area/forecast/2844600', 'name': '神河町'}, {'link': 'http://weather.livedoor.com/area/forecast/2846400', 'name': '太子町'}, {'link': 'http://weather.livedoor.com/area/forecast/2848100', 'name': '上郡町'}, {'link': 'http://weather.livedoor.com/area/forecast/2850100', 'name': '佐用町'}], 'link': 'http://weather.livedoor.com/area/forecast/280010', 'forecasts': [{'dateLabel': '今日', 'telop': '晴れ', 'date': '2019-04-03', 'temperature': {'min': None, 'max': None}, 'image': {'width': 50, 'url': 'http://weather.livedoor.com/img/icon/1.gif', 'title': '晴れ', 'height': 31}}, {'dateLabel': '明日', 'telop': '晴れ', 3'date': '2019-04-04', 'temperature': {'min': {'celsius': '7', 'fahrenheit': '44.6'}, 'max': {'celsius': '15', 'fahrenheit': '59.0'}}, 'image': {'width': 50, 'url': 'http://weather.livedoor.com/img/icon/1.gif', 'title': '晴れ', 'height': 31}}, {'dateLabel': '明後日', 'telop': '晴時々曇', 'date': '2019-04-05', 'temperature': {'min': None, 'max': None}, 'image': {'width': 50, 'url': 'http://weather.livedoor.com/img/icon/2.gif', 'title': '晴時々曇', 'height': 31}}], 'location': {'city': '神戸', 'area': '近畿', 'prefecture': '兵庫県'}, 'publicTime': '2019-04-03T17:00:00+0900', 'copyright': {'provider': [{'link': 'http://tenki.jp/', 'name': '日本気象協会'}], 'link': 'http://weather.livedoor.com/', 'title': '(C) LINE Corporation', 'image': {'width': 118, 'link': 'http://weather.livedoor.com/', 'url': 'http://weather.livedoor.com/img/cmn/livedoor.gif', 'title': 'livedoor 天気情報', 'height': 26}}, 'title': '兵庫県 神戸 の天気', 'description': {'text': ' 近畿地方は、高気圧に覆われておおむね晴れていますが、寒気の影響によ\nり、北部や中部では雨や雪の降っている所があります。\n\n 今夜から 4明日の兵庫県は、高気圧に覆われておおむね晴れるでしょう。', 'publicTime': '2019-04-03T16:32:00+0900'}}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/04 00:33