본문 바로가기

디스코드 봇 개발일지

디스코드 자작봇 개발일지#7

사진이 안보이신다면 https://blog.naver.com/turnbf/221374834781 에서 봐주세요.

오늘은 !날씨 명령어의 기능을 개선하였다.



개선하기전 실행모습

개선후 실행모습

개선하기전 내일 오전/오후 날씨 기능이 오류가있었다.
내일이 아닌 오늘의 오전,오후 날씨를 불러왔던 것이었다.
그래서 오후의 날씨로 다시 불러오고
조금더 구체적으로 날씨정보를 제공하는 기능으로 개선하였다.

if message.content.startswith("!날씨"): learn = message.content.split(" ") location = learn[1] enc_location = urllib.parse.quote(location+'날씨') hdr = {'User-Agent': 'Mozilla/5.0'} url = 'https://search.naver.com/search.naver?where=nexearch&sm=top_hty&fbm=1&ie=utf8&query=' + enc_location print(url) req = Request(url, headers=hdr) html = urllib.request.urlopen(req) bsObj = bs4.BeautifulSoup(html, "html.parser") todayBase = bsObj.find('div', {'class': 'main_info'}) todayTemp1 = todayBase.find('span', {'class': 'todaytemp'}) todayTemp = todayTemp1.text.strip() # 온도 print(todayTemp) todayValueBase = todayBase.find('ul', {'class': 'info_list'}) todayValue2 = todayValueBase.find('p', {'class': 'cast_txt'}) todayValue = todayValue2.text.strip() # 밝음,어제보다 ?도 높거나 낮음을 나타내줌 print(todayValue) todayFeelingTemp1 = todayValueBase.find('span', {'class': 'sensible'}) todayFeelingTemp = todayFeelingTemp1.text.strip() # 체감온도 print(todayFeelingTemp) todayMiseaMongi1 = bsObj.find('div', {'class': 'sub_info'}) todayMiseaMongi2 = todayMiseaMongi1.find('div', {'class': 'detail_box'}) todayMiseaMongi3 = todayMiseaMongi2.find('dd') todayMiseaMongi = todayMiseaMongi3.text # 미세먼지 print(todayMiseaMongi) tomorrowBase = bsObj.find('div', {'class': 'table_info weekly _weeklyWeather'}) tomorrowTemp1 = tomorrowBase.find('li', {'class': 'date_info'}) tomorrowTemp2 = tomorrowTemp1.find('dl') tomorrowTemp3 = tomorrowTemp2.find('dd') tomorrowTemp = tomorrowTemp3.text.strip() # 오늘 오전,오후온도 print(tomorrowTemp) tomorrowAreaBase = bsObj.find('div', {'class': 'tomorrow_area'}) tomorrowMoring1 = tomorrowAreaBase.find('div', {'class': 'main_info morning_box'}) tomorrowMoring2 = tomorrowMoring1.find('span', {'class': 'todaytemp'}) tomorrowMoring = tomorrowMoring2.text.strip() # 내일 오전 온도 print(tomorrowMoring) tomorrowValue1 = tomorrowMoring1.find('div', {'class': 'info_data'}) tomorrowValue = tomorrowValue1.text.strip() # 내일 오전 날씨상태, 미세먼지 상태 print(tomorrowValue) tomorrowAreaBase = bsObj.find('div', {'class': 'tomorrow_area'}) tomorrowAllFind = tomorrowAreaBase.find_all('div', {'class': 'main_info morning_box'}) tomorrowAfter1 = tomorrowAllFind[1] tomorrowAfter2 = tomorrowAfter1.find('p', {'class': 'info_temperature'}) tomorrowAfter3 = tomorrowAfter2.find('span', {'class': 'todaytemp'}) tomorrowAfterTemp = tomorrowAfter3.text.strip() # 내일 오후 온도 print(tomorrowAfterTemp) tomorrowAfterValue1 = tomorrowAfter1.find('div', {'class': 'info_data'}) tomorrowAfterValue = tomorrowAfterValue1.text.strip() print(tomorrowAfterValue) # 내일 오후 날씨상태,미세먼지 embed = discord.Embed( title=learn[1]+ ' 날씨 정보', description=learn[1]+ '날씨 정보입니다.', colour=discord.Colour.gold() ) embed.add_field(name='현재온도', value=todayTemp+'˚', inline=False) # 현재온도 embed.add_field(name='체감온도', value=todayFeelingTemp, inline=False) # 체감온도 embed.add_field(name='현재상태', value=todayValue, inline=False) # 밝음,어제보다 ?도 높거나 낮음을 나타내줌 embed.add_field(name='현재 미세먼지 상태', value=todayMiseaMongi, inline=False) # 오늘 미세먼지 embed.add_field(name='오늘 오전/오후 날씨', value=tomorrowTemp, inline=False) # 오늘날씨 # color=discord.Color.blue() embed.add_field(name='**----------------------------------**',value='**----------------------------------**', inline=False) # 구분선 embed.add_field(name='내일 오전온도', value=tomorrowMoring+'˚', inline=False) # 내일오전날씨 embed.add_field(name='내일 오전날씨상태, 미세먼지 상태', value=tomorrowValue, inline=False) # 내일오전 날씨상태 embed.add_field(name='내일 오후온도', value=tomorrowAfterTemp + '˚', inline=False) # 내일오후날씨 embed.add_field(name='내일 오후날씨상태, 미세먼지 상태', value=tomorrowAfterValue, inline=False) # 내일오후 날씨상태 await client.send_message(message.channel,embed=embed)


개선후 !날씨 명령어의 코드이다.

봇 소스입니다.