-
파이썬 웹 스크래핑_ 날씨 정보 가져오기마케팅/파이썬 2023. 6. 9. 15:28
1. 날씨 정보 가져오기
q=다음 검색 키워드가 연결되어 있음
https://search.daum.net/search?w=tot&DA=YZR&t__nil_searchbox=btn&sug=&sugo=&sq=&o=&q=서울시+종로구+청운동+날씨
1-1. 검색 결과를 가져오는 코드 만들기
import requests from bs4 import BeautifulSoup location="서울시 종로구 청운동" search_query= location+" 날씨" search_url="https://search.daum.net/search?w=tot&DA=YZR&t__nil_searchbox=btn&sug=&sugo=&sq=&o=&q=" url=search_url+search_query html_weather=requests.get(url).text soup_weather=BeautifulSoup(html_weather, "lxml") print(url)
1-2. 여러 요소 중 온도만 가져오기
import requests from bs4 import BeautifulSoup location="서울시 종로구 청운동" search_query= location+" 날씨" search_url="https://search.daum.net/search?w=tot&DA=YZR&t__nil_searchbox=btn&sug=&sugo=&sq=&o=&q=" url=search_url+search_query html_weather=requests.get(url).text soup_weather=BeautifulSoup(html_weather, "lxml") txt_temp=soup_weather.select_one('strong.txt_temp').get_text() txt_temp
1-3. 날씨 설명만 가져오기
import requests from bs4 import BeautifulSoup location="서울시 종로구 청운동" search_query= location+" 날씨" search_url="https://search.daum.net/search?w=tot&DA=YZR&t__nil_searchbox=btn&sug=&sugo=&sq=&o=&q=" url=search_url+search_query html_weather=requests.get(url).text soup_weather=BeautifulSoup(html_weather, "lxml") txt_weather=soup_weather.select_one('span.txt_weather').get_text() txt_weathe
1-3. 풍속, 습도, 미세먼지 데이터 가져오기
상위 태그 <dl class="dl_weather"> ~<dl>
select('dl.dl_weather dd') 이용하여 dd태그로 구성된 현재 풍속, 현재 습도, 미세먼지 데이터 가져오기
import requests from bs4 import BeautifulSoup location="서울시 종로구 청운동" search_query= location+" 날씨" search_url="https://search.daum.net/search?w=tot&DA=YZR&t__nil_searchbox=btn&sug=&sugo=&sq=&o=&q=" url=search_url+search_query html_weather=requests.get(url).text soup_weather=BeautifulSoup(html_weather, "lxml") dl_weather_dds=soup_weather.select('dl.dl_weather dd') dl_weather_dds
1-4. get_weather_daum()을 사용하여 날씨 정보 함수 호출하기
'마케팅 > 파이썬' 카테고리의 다른 글
파이썬 웹 스크레이핑 기초 (0) 2023.06.05 파이썬 기초8. 딕셔너리, 집합 (0) 2023.06.01 [파이썬 웹 스크래핑] 페이지 주소 가져오기 (0) 2023.06.01 파이썬 기초7. 함수 (1) 2023.05.30 파이썬 기초6. 리스트(list), remove, append, insert, del, sorted, reverse (0) 2023.05.26