-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
54 lines (42 loc) · 2.13 KB
/
main.py
File metadata and controls
54 lines (42 loc) · 2.13 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from bs4 import BeautifulSoup
import requests
import sys
from time import ctime
import json
import os
current_hour = int(ctime()[11: 13])
try:
city = sys.argv[1]
area = sys.argv[2]
except:
print("API輸入錯誤,輸入如下: python main.py (縣市) (鄉鎮市區)\n 範例:python main.py 臺南市 北區")
exit()
headers={'Content-type': 'text/plain; charset=utf-8'}
def weather(city, area):
#===Preprocess===
respond = requests.get(f"https://www.msn.com/zh-tw/weather/hourlyforecast/in-{area}, {city}", headers= headers)
respond2 = requests.get(f"https://tw.news.yahoo.com/weather?location-picker={city}+{area}", headers= headers)
soup = BeautifulSoup(respond.text, "html.parser")
soup2 = BeautifulSoup(respond2.text, "html.parser")
#===Getting data===
raining_rate = (soup.find_all(class_ = "rowItemText-DS-cwphqS")[5]).text
tomr_temp = str(soup.find_all(class_ = "rowItemText-DS-cwphqS")[4* (current_hour+(current_hour-24))])
tomr_temp = tomr_temp[tomr_temp.index(">")+1: tomr_temp.index("°")+1]
UV_index = soup2.find_all(class_ = "D(f) Py(8px) Bdb Bdbs(d) Bdbw(1px) Bdbc(t) Jc(sb)")[0].find("dd").text
vision = soup2.find_all(class_ = "D(f) Py(8px) Bdb Bdbs(d) Bdbw(1px) Bdbc($weatherBorderColor) Jc(sb)")[-1].find_all("dd")[-1].text
humidity = soup2.find_all(class_ = "D(f) Py(8px) Bdb Bdbs(d) Bdbw(1px) Bdbc($weatherBorderColor) Jc(sb)")[1].find("dd").text
weather_status = {"縣市": city, "鄉鎮市區": area,
"溫度": soup.find(class_ = "firstLineText-DS-XwdBJw").string,
"降雨機率": raining_rate,
"未來三小時溫度": soup.find_all(class_ = "rowItemText-DS-cwphqS")[12].string,
"紫外線指數": UV_index,
"能見度": vision,
"濕度": humidity,
"明天溫度(同個時間點)": tomr_temp
}
return weather_status
if __name__ == "__main__":
output = w = json.dumps(weather(city, area), ensure_ascii=False, indent=4)
json_path = open(f"{os.getcwd()}\\outcome.json", "w", encoding="utf8")
json_path.write(output)
json_path.close()