See the Weather in Any City Right Now using Python’s OpenWeatherMap API
Weather information, such as current conditions, predictions, and historical data, is available to developers of online services and mobile apps thanks to the OpenWeatherMap.
It provides a restricted free tier of service in addition to an API with support for JSON, XML, and HTML. Long-range predictions, visual maps, and up-to-the-minute weather data may all be requested by users (showing cloud cover, wind speed, pressure, and precipitation). The current conditions in any city may be determined using Python’s OpenWeatherMap API.
The JSON Module Request Format and the
necessary.
Method 1: Using the request Module and json:
Request and JSON are used in this approach.
module.
Its Output is
-
# import the necessary components
-
import
requests, json
-
# Entering of our API key here into it
-
api_key =
“Entering of the API”
-
# base url variable for url storage
-
base_urlin =
“http://api.openweathermap.org/data/2.5/weather?”
-
# Give in the city name into it
-
city_namein = input(
“Entering up the city name : ”
)
-
# complete_url to be variable to in the store
-
# complete url to be inserted for address
-
complete_urlin = base_urlin +
“appidin=”
+ api_keyin +
“&q=”
+ city_namein
-
# return response object for the get function of requests module
-
response = requests.get(complete_urlin)
-
# Change json form data into
-
# python format data using the json method
-
#of the response object.
-
x = response.json()
-
# Currently, x includes a list of nested dictionaries.
-
# A value of “cod” equal to “404”
-
#indicates that the city has been located;
-
#otherwise, the city has not been located.
-
if
x[
“cod”
] !=
“404”
:
-
# the “main” # key’s value in the variable y
-
y = x[
“main”
]
-
# Save the value that goes with the “temp” key of y.
-
current_temperaturein = y[
“temp”
]
-
#Keep the value associated with the “pressure” key of y in memory. current_pressurein = y[“pressure”]
-
# storing up the value corresponding
-
# to the y-“humidity” note’s
-
current_humidityin = y[
“humidity”
]
-
# the value of “weather” should be saved in the variable z.
-
z = x[
“weather”
]
-
# storing up the value corresponding
-
#in the “description” key at the index 0 of z
-
weather_descriptionin = z[
0
][
“description”
]
-
# printing up the following values
-
print
(
” Temperature (in kelvin unit) = ”
+
-
str(current_temperaturein) +
-
“\n atmospheric pressure display(in hPa unit) = ”
+
-
str(current_pressurein) +
-
“\n humidity display(in percentage) = ”
+ str(current_humidityin) +
-
“\n description = ”
+ str(weather_descriptionin))
-
else
:
-
print
(
” City Unknown ”
)
Enter city name : Mumbai Temperature display(in kelvin unit) = 318.15 atmospheric pressure display(in hPa unit) = 956 humidity display(in percentage) = 45 description = Cloudy
Method 2: Using the request Module and BeautifulSoup
Request and BeautifulSoup will be used in this approach.
module.
Input
-
#importing needed files
-
from
bs4
import
BeautifulSoup
-
#importing requests
-
import
requests
-
headers = {
-
‘User-Agent’
:
‘Mozilla/5.0 (Windows NT 10.0; Win32; x32) AppleWebKit/537.36 (KHTML, like Gecko) Brave/58.0.3029.110 Safari/537.3’
}
-
def
weather(place):
-
place = place.replace(
” ”
,
“+”
)
-
res = requests.get(
-
f
‘https://www.google.com/search?q={city}&oq={city}&aqs=chrome.0.36t89l2j0l4j89d5s660.6128j1j7&sourceid=chrome&ie=UTF-8’
, headers=headers)
-
print
(
“Searching for..\n”
)
-
s = BeautifulSoup(res.text,
‘html.parser’
)
-
l = s.select(
‘#wob_loc’
)[
0
].getText().strip()
-
t =s.select(
‘#wob_dts’
)[
0
].getText().strip()
-
i= s.select(
‘#wob_dc’
)[
0
].getText().strip()
-
w = s.select(
‘#wob_tm’
)[
0
].getText().strip()
-
print
(l)
-
print
(t)
-
print
(i)
-
print
(w+
“°C”
)
-
place = input(
“Enter up the Name of City: ”
)
-
place = place+
” w”
-
w (place)
-
print
(
“Have a good Day:)”
)
Production of
-
Hyderabad
Explanation:
Enter the Name of City: Hyderabad Searching for... Hyderabad, Telangana Wednesday, 10:00 pm Partly Cloudy 28°C Have a Good Day:)
In this case, we’ll implement some of the second strategy’s modules and features,
Web scraping using BeautifulSoup, a Python tool, is a great way to get information out of HTML and XML files. In order to access data in a more logical and structured fashion, it builds a parse tree from the page’s source code. To add some tasty soup to your system, enter the code below into the terminal.
library.
Requests
In this scenario, we’ll utilise Python’s requests module to send HTTP requests. To set up, enter this code into the terminal.
We’re making use of headers since they precede the raw message, which is data downloaded from the website and include protocol-specific information.
Finally, we’ll use the get() function to request the information from Google by passing it the query and the location’s name. Then, BeautifulSoup will extract the relevant HTML data from the page.
The selected information, including time, information, and location, will be retrieved from a variable using the select() function after being temporarily stored there.