和風api爬取天氣預報數據


'''
和風api爬取天氣預報數據
目標:https://free-api.heweather.net/s6/weather/forecast?key=cc33b9a52d6e48de852477798980b76e&location=CN101090101
   得到中國城市的代碼:https://a.hecdn.net/download/dev/china-city-list.csv
  目前先查20個城市第二天的天氣
'''
import requests
url = "https://a.hecdn.net/download/dev/china-city-list.csv"
strhtml = requests.get(url=url)
data = strhtml.text
data1 = data.split("\n")
for i in range(3):
    data1.remove(data1[0])

# 測試城市:石家庄,得到明天的天氣情況
# url = 'https://free-api.heweather.net/s6/weather/forecast?key=cc33b9a52d6e48de852477798980b76e&location=CN101090101'
# strhtml  = requests.get(url)
# dict=strhtml.json()
# weather_t = []
# for item in dict["HeWeather6"][0]['daily_forecast'][1:2]:
#     weather = {
#         '城市名':dict["HeWeather6"][0]['basic']['location'],
#         '日期':item['date'],
#         '白天':item['cond_txt_d'],
#         '夜間':item['cond_txt_n']
#        }
#     weather_t.append(weather)
# print(weather_t)

for item in data1[:20]:
    url = 'https://free-api.heweather.net/s6/weather/forecast?key=cc33b9a52d6e48de852477798980b76e&location='+item[0:11]
    strhtml  = requests.get(url)
    dict=strhtml.json()
    weather_t = []
    for item in dict["HeWeather6"][0]['daily_forecast'][1:2]:
        weather = {
            '城市名':dict["HeWeather6"][0]['basic']['location'],
            '今天日期':dict["HeWeather6"][0]['daily_forecast'][0]['date'],
            'utc':dict["HeWeather6"][0]['daily_forecast'][0]['mr'],
            'loc':dict["HeWeather6"][0]['daily_forecast'][0]['ms'],
            '明天日期':item['date'],
            '白天':item['cond_txt_d'],
            '夜間':item['cond_txt_n']
           }
        weather_t.append(weather)
    print(weather_t)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM