在Python中使用 OpenWeathermap API 查找任何城市的当前天气
这 打开天气地图 是一项向 Web 服务和移动应用程序的开发人员提供天气数据的服务,包括当前天气数据、预报和历史数据。
它提供了一个带有 JSON、XML 和 HTML 端点的 API 以及有限的免费使用层。每分钟拨打超过 60 个电话需要每月40美元起的付费订阅。访问历史数据需要每月150 美元的订阅费。用户可以请求当前天气信息、扩展预报和图形地图(显示云量、风速、压力和降水)。
要使用这个当前的天气数据 API,必须需要 API 密钥,可以从这里获取。
注意:用户需要在 openweathermap.org 上创建一个帐户,然后才能使用 API。
需要的模块:
requests
json
下面是实现:
Python3
# Python program to find current
# weather details of any city
# using openweathermap api
# import required modules
import requests, json
# Enter your API key here
api_key = "Your_API_Key"
# base_url variable to store url
base_url = "http://api.openweathermap.org/data/2.5/weather?"
# Give city name
city_name = input("Enter city name : ")
# complete_url variable to store
# complete url address
complete_url = base_url + "appid=" + api_key + "&q=" + city_name
# get method of requests module
# return response object
response = requests.get(complete_url)
# json method of response object
# convert json format data into
# python format data
x = response.json()
# Now x contains list of nested dictionaries
# Check the value of "cod" key is equal to
# "404", means city is found otherwise,
# city is not found
if x["cod"] != "404":
# store the value of "main"
# key in variable y
y = x["main"]
# store the value corresponding
# to the "temp" key of y
current_temperature = y["temp"]
# store the value corresponding
# to the "pressure" key of y
current_pressure = y["pressure"]
# store the value corresponding
# to the "humidity" key of y
current_humidity = y["humidity"]
# store the value of "weather"
# key in variable z
z = x["weather"]
# store the value corresponding
# to the "description" key at
# the 0th index of z
weather_description = z[0]["description"]
# print following values
print(" Temperature (in kelvin unit) = " +
str(current_temperature) +
"\n atmospheric pressure (in hPa unit) = " +
str(current_pressure) +
"\n humidity (in percentage) = " +
str(current_humidity) +
"\n description = " +
str(weather_description))
else:
print(" City Not Found ")
Python3
from bs4 import BeautifulSoup
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
def weather(city):
city = city.replace(" ", "+")
res = requests.get(
f'https://www.google.com/search?q={city}&oq={city}&aqs=chrome.0.35i39l2j0l4j46j69i60.6128j1j7&sourceid=chrome&ie=UTF-8', headers=headers)
print("Searching...\n")
soup = BeautifulSoup(res.text, 'html.parser')
location = soup.select('#wob_loc')[0].getText().strip()
time = soup.select('#wob_dts')[0].getText().strip()
info = soup.select('#wob_dc')[0].getText().strip()
weather = soup.select('#wob_tm')[0].getText().strip()
print(location)
print(time)
print(info)
print(weather+"°C")
city = input("Enter the Name of City -> ")
city = city+" weather"
weather(city)
print("Have a Nice Day:)")
# This code is contributed by adityatri
输出 :
Enter city name : Delhi
Temperature (in kelvin unit) = 312.15
atmospheric pressure (in hPa unit) = 996
humidity (in percentage) = 40
description = haze
方法二:
在第二种方法中,我们将使用以下一些模块和功能,如下所示:
1. BeautifulSoup:它是Python中的一个库,用于从HTML和XML文件中提取数据,即用于网络抓取目的。它从页面源代码生成解析树,可用于以更具可读性和层次性的方式提取数据。要在系统中安装漂亮的汤库,请在终端中使用以下代码:-
-->> pip install beautifulsoup
2. Requests:这里我们将使用 Python 的 requests 模块来发出 HTTP 请求。在终端中使用下面的代码进行安装。
-->> pip install requests
3.这里我们使用标头,因为标头包含特定于协议的信息,这些信息位于原始消息之前,即从网站检索。
4.之后,我们将使用get()函数并在其中传递 google 搜索以及城市名称,以从 google 检索数据。
然后我们将使用 beautifulsoup 并解析网站所需的 HTML 数据。
5.然后我们将使用select()函数检索特定信息,如时间、信息、位置,将它们存储在某个变量中,并进一步存储它们。
在这一切之后,我们最终将打印存储在变量中的数据。
Python3
from bs4 import BeautifulSoup
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
def weather(city):
city = city.replace(" ", "+")
res = requests.get(
f'https://www.google.com/search?q={city}&oq={city}&aqs=chrome.0.35i39l2j0l4j46j69i60.6128j1j7&sourceid=chrome&ie=UTF-8', headers=headers)
print("Searching...\n")
soup = BeautifulSoup(res.text, 'html.parser')
location = soup.select('#wob_loc')[0].getText().strip()
time = soup.select('#wob_dts')[0].getText().strip()
info = soup.select('#wob_dc')[0].getText().strip()
weather = soup.select('#wob_tm')[0].getText().strip()
print(location)
print(time)
print(info)
print(weather+"°C")
city = input("Enter the Name of City -> ")
city = city+" weather"
weather(city)
print("Have a Nice Day:)")
# This code is contributed by adityatri
样本输入:
Mahoba
样本输出:
Enter the Name of City -> Mahoba
Searching...
Mahoba, Uttar Pradesh
Monday, 12:00 am
Cloudy
27°C
Have a Nice Day:)