Skip to content

Commit e3b63cf

Browse files
committed
added extracting weather tutorial
1 parent 79be410 commit e3b63cf

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
3838
- ### [Web Scraping](https://www.thepythoncode.com/topic/web-scraping)
3939
- [How to Access Wikipedia in Python](https://www.thepythoncode.com/article/access-wikipedia-python). ([code](general/wikipedia-extractor))
4040
- [How to Extract YouTube Data in Python](https://www.thepythoncode.com/article/get-youtube-data-python). ([code](general/youtube-extractor))
41+
- [How to Extract Weather Data from Google in Python](https://www.thepythoncode.com/article/extract-weather-data-python). ([code](general/weather-extractor))
4142

general/weather-extractor/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# [How to Extract Weather Data from Google in Python](https://www.thepythoncode.com/article/extract-weather-data-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`
4+
- To get the Google weather data of your current region you live in:
5+
```
6+
python weather.py
7+
```
8+
- To get the weather of a specific region in the world:
9+
```
10+
python weather.py "New York"
11+
```
12+
This will grab the weather information of "New York" state in the US.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
requests
2+
bs4

general/weather-extractor/weather.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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

Comments
 (0)