【发布时间】:2021-05-16 16:31:28
【问题描述】:
我正在尝试从一堆不同的邮政编码中提取此 API 的平均温度。我目前可以通过手动更改 API 的 URL 中的邮政编码来做到这一点,但我希望它能够遍历邮政编码列表或要求输入并使用这些邮政编码。 但是,我很新,不知道如何将变量和东西添加到链接中,或者我过于复杂了。所以基本上我正在寻找一些方法来向链接中添加变量或具有相同效果的东西,以便我可以随时更改它。
import urllib.request
import json
out = open("output.txt", "w")
link = "http://api.openweathermap.org/data/2.5/weather?zip={zip-code},us&appid={api-key}"
print(link)
x = urllib.request.urlopen(link)
url = x.read()
out.write(str(url, 'utf-8'))
returnJson = json.loads(url)
print('\n')
print(returnJson["main"]["temp"])
【问题讨论】: