안녕하세요 "귀엽고 깜찍한(?) Binary"입니다.
(헤헷)
음 오늘은 날씨 소스를 "파싱"으로 만들어볼거에요.
네이버, 다음, 구글이 있는데 전 네이버로 할래요 전 네이버가 좋으니까요?
그러면 네이버 페이지에 접속하여 검색을 해봅시다.
"~~ 날씨"라고 검색할게요
한글로 검색해야하기 때문에,
encodeURIComponent()를 사용해줄 겁니다.
자 그럼 이제 검색을 해보죠,
그리고 클릭하여,
이렇게 복사를 합니다.
"#main_pack > section.sc_new.cs_weather._weather > div > div.api_cs_wrap > div.weather_box > div.weather_area._mainArea > div.today_area._mainTabContent > div.main_info > div > p > span.todaytemp"
이거네요
이제 파싱을 해봅시다.
org.jsoup.Jsoup.connect("naver.com").get().select("#main_pack > section.sc_new.cs_weather._weather > div > div.api_cs_wrap > div.weather_box > div.weather_area._mainArea > div.today_area._mainTabContent > div.main_info > div > p > span.todaytemp")
자 이렇게 온도 파싱은 끝냈습니다.
이제 문장 파싱을 해볼까요오~
"#main_pack > section.sc_new.cs_weather._weather > div > div.api_cs_wrap > div.weather_box > div.weather_area._mainArea > div.today_area._mainTabContent > div.main_info > div > ul > li:nth-child(1) > p"
그건 이거네요..
org.jsoup.Jsoup.connect("naver.com").get().select("#main_pack > section.sc_new.cs_weather._weather > div > div.api_cs_wrap > div.weather_box > div.weather_area._mainArea > div.today_area._mainTabContent > div.main_info > div > ul > li:nth-child(1) > p")
자 이제 소스를 만들어봅시다.
function response(room, msg, sender, isGroupChat, replier, imageDB, packageName) {
if (msg.startsWith("!날씨 ")) {
let isarea = encodeURIComponent(msg.slice(4)+" 날씨");
let area = "https://search.naver.com/search.naver?where=nexearch&sm=top_hty&fbm=0&ie=utf8&query=" + isarea;
let temp = org.jsoup.Jsoup.connect(area).get().select("#main_pack > section.sc_new.cs_weather._weather > div > div.api_cs_wrap > div.weather_box > div.weather_area._mainArea > div.today_area._mainTabContent > div.main_info > div > p > span.todaytemp")
let word = org.jsoup.Jsoup.connect(area).get().select("#main_pack > section.sc_new.cs_weather._weather > div > div.api_cs_wrap > div.weather_box > div.weather_area._mainArea > div.today_area._mainTabContent > div.main_info > div > ul > li:nth-child(1) > p");
replier.reply(msg.slice(4) + "의 날씨\n\n" + temp + "도\n" + word)
}
}
실제 실행 화면을 보여드리겠습니다.
이렇게 됬슴미다
그럼 저는 이만.. 공감 버튼 한번씩만 눌러주세요
로그인 필요없어요오!
'카톡봇 🤖 > 강좌 👨🏫' 카테고리의 다른 글
(강의) 카카오톡 봇 강좌, 작동 원리와 쓰는 앱 - #1 (2) | 2021.08.16 |
---|---|
카카오톡 영구정지 최대한 빠르게 푸는방법 (0) | 2021.03.02 |
카톡봇 13분만에 성능 늘리고 최적화 하기 (0) | 2021.02.11 |
[ 강좌 ] 카카오링크 강좌(API를 사용하여 봇으로 전송하기!) #1 (1) | 2021.02.11 |
[ 강좌 ] 3. JSON으로 사용자 데이터 저장하기 / 삭제하기 (7) | 2021.01.12 |