-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4-2.py
More file actions
34 lines (28 loc) · 891 Bytes
/
Copy path4-2.py
File metadata and controls
34 lines (28 loc) · 891 Bytes
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
import urllib.request as req
from bs4 import BeautifulSoup
import os.path
url = "https://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=108"
savename = "d:/bbb/forecast.xml"
if not os.path.exists(savename):
req.urlretrieve(url, savename)
xml = open(savename, 'r', encoding="utf-8").read()
soup = BeautifulSoup(xml, 'html.parser')
info = {}
for location in soup.find_all("location"):
loc = location.find("city").string
#print(loc)
weather = location.find_all("tmn")
#print(weather)
if not (loc in info):
info[loc] = []
for tmn in weather:
info[loc].append(tmn.string)
#print(info)
#print(info.keys())
with open('d:/bbb/forecast.txt','wt') as f:
for loc in sorted(info.keys()):
print("+", loc)
f.write(str(loc) + '\n')
for n in info[loc]:
print("-", n)
f.write ('\t' + str(n) +'\n')