1
+
2
+ from bs4 import BeautifulSoup as bs
3
+ import requests
4
+
5
+ USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"
6
+ # US english
7
+ LANGUAGE = "en-US,en;q=0.5"
8
+
9
+ def get_weather_data (url ):
10
+ session = requests .Session ()
11
+ session .headers ['User-Agent' ] = USER_AGENT
12
+ session .headers ['Accept-Language' ] = LANGUAGE
13
+ session .headers ['Content-Language' ] = LANGUAGE
14
+ html = session .get (url )
15
+ # create a new soup
16
+ soup = bs (html .text , "html.parser" )
17
+ # store all results on this dictionary
18
+ result = {}
19
+ # extract region
20
+ result ['region' ] = soup .find ("div" , attrs = {"id" : "wob_loc" }).text
21
+ # extract temperature now
22
+ result ['temp_now' ] = soup .find ("span" , attrs = {"id" : "wob_tm" }).text
23
+ # get the day and hour now
24
+ result ['dayhour' ] = soup .find ("div" , attrs = {"id" : "wob_dts" }).text
25
+ # get the actual weather
26
+ result ['weather_now' ] = soup .find ("span" , attrs = {"id" : "wob_dc" }).text
27
+ # get the precipitation
28
+ result ['precipitation' ] = soup .find ("span" , attrs = {"id" : "wob_pp" }).text
29
+ # get the % of humidity
30
+ result ['humidity' ] = soup .find ("span" , attrs = {"id" : "wob_hm" }).text
31
+ # extract the wind
32
+ result ['wind' ] = soup .find ("span" , attrs = {"id" : "wob_ws" }).text
33
+ # get next few days' weather
34
+ next_days = []
35
+ days = soup .find ("div" , attrs = {"id" : "wob_dp" })
36
+ for day in days .findAll ("div" , attrs = {"class" : "wob_df" }):
37
+ # extract the name of the day
38
+ day_name = day .find ("div" , attrs = {"class" : "vk_lgy" }).attrs ['aria-label' ]
39
+ # get weather status for that day
40
+ weather = day .find ("img" ).attrs ["alt" ]
41
+ temp = day .findAll ("span" , {"class" : "wob_t" })
42
+ # maximum temparature in Celsius, use temp[1].text if you want fahrenheit
43
+ max_temp = temp [0 ].text
44
+ # minimum temparature in Celsius, use temp[3].text if you want fahrenheit
45
+ min_temp = temp [2 ].text
46
+ next_days .append ({"name" : day_name , "weather" : weather , "max_temp" : max_temp , "min_temp" : min_temp })
47
+ # append to result
48
+ result ['next_days' ] = next_days
49
+ return result
50
+
51
+
52
+ if __name__ == "__main__" :
53
+ URL = "https://www.google.com/search?lr=lang_en&ie=UTF-8&q=weather"
54
+ import argparse
55
+ parser = argparse .ArgumentParser (description = "Quick Script for Extracting Weather data using Google Weather" )
56
+ parser .add_argument ("region" , nargs = "?" , help = """Region to get weather for, must be available region.
57
+ Default is your current location determined by your IP Address""" , default = "" )
58
+ # parse arguments
59
+ args = parser .parse_args ()
60
+ region = args .region
61
+ URL += region
62
+ # get data
63
+ data = get_weather_data (URL )
64
+ # print data
65
+ print ("Weather for:" , data ["region" ])
66
+ print ("Now:" , data ["dayhour" ])
67
+ print (f"Temperature now: { data ['temp_now' ]} °C" )
68
+ print ("Description:" , data ['weather_now' ])
69
+ print ("Precipitation:" , data ["precipitation" ])
70
+ print ("Humidity:" , data ["humidity" ])
71
+ print ("Wind:" , data ["wind" ])
72
+ print ("Next days:" )
73
+ for dayweather in data ["next_days" ]:
74
+ print ("=" * 40 , dayweather ["name" ], "=" * 40 )
75
+ print ("Description:" , dayweather ["weather" ])
76
+ print (f"Max temperature: { dayweather ['max_temp' ]} °C" )
77
+ print (f"Min temperature: { dayweather ['min_temp' ]} °C" )
0 commit comments