和风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)

 

转载于:https://www.cnblogs.com/chrysanthemum/p/11470569.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值