본문으로 바로가기

경돌이 코드 - weatherUpdate.py

category 카테고리 없음 2018. 8. 23. 11:00

weatherUpdate.py


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# -*- coding: utf-8 -*-
import requests
import os
from bs4 import BeautifulSoup
 
#현재날씨 요청
url = "https://search.daum.net/search"
params={
        'w' : 'tot',
        'q' : '광교동 날씨'
}
res = requests.get(url, params = params)
html = res.text
 
#날씨 파트만 추출
soup = BeautifulSoup(html, "lxml")
weath = soup.find("div",{"class" : "cont_today"})
 
#현재기온 추출
now = weath.find('span',{'class':'desc_temp'})
txt = now.find('span',{'class':'txt_weather'}).string.encode('utf-8')
temp = now.find('strong').get_text().encode('utf-8')
 
print("["+txt+"]\n")
print("- 기온")
print("  "+temp)
 
#현재 기상정보 추출
element = weath.find_all('dl')
 
for i in element:
   title = i.find('span',{'class':'txt_tit'}).string.encode('utf-8')
   value = i.find('dd').string.encode('utf-8')
   print("\n- "+title)
   print("  "+value)
cs