-
카테고리
-
세부 분야
프로그래밍 언어
-
해결 여부
미해결
네이버 날씨 알리미
22.02.25 23:09 작성 조회수 195
1
ㅠㅠ 강사님 네이버 날씨가 기존에 있던 div 정보가 아니라서
print 하면 [] <=== 요 정보만 나오네요 ㅠㅠ
어떤 정보를 가지고 오는 것이 좋을까요?
답변을 작성해보세요.
0
남박사
지식공유자2022.02.26
네 말씀하신데로 네이버 페이지가 변경되었네요. 일단 강의 내용 페이지에 수정된 코드를 올려놨습니다. 분석하는 방식이나 흐름은 크게 달라진거 없으니 수정된 코드를 참고해 어떻게 변경되었는지 비교해보시는것도 좋은 공부가 될 듯 합니다.
def get_weather(where):
weather = ""
url = "https://search.naver.com/search.naver?query={}+날씨".format(where)
r = requests.get(url)
bs = BeautifulSoup(r.text, "lxml")
w_box = bs.select_one("div.weather_info > div.status_wrap")
if w_box:
temp = w_box.select_one("div.temperature_text")
spans = temp.select("span")
[s.decompose() for s in spans]
temperature = temp.select_one("strong").text
w_text = w_box.select_one("div.temperature_info").text
w_text = ' '.join(w_text.split())
lists = w_box.select("ul.today_chart_list > li")
w_list = []
for li in lists:
w_string = li.select_one("a").text.strip()
w_list.append(w_string)
weather = "{}℃\r\n{}\r\n{}".format(temperature, w_text, "\n".join(w_list))
return weather
답변 1