zoukankan      html  css  js  c++  java
  • 和风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("
    ")
    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)
  • 相关阅读:
    31 把数组排成最小的数 + 仿函数的写法就记忆这个就行了
    30 整数中1出现的次数(从1到n整数中1出现的次数)这题很难要多看*
    29 连续子数组的最大和
    c/c++ struct的大小以及sizeof用法
    28 最小的K个数
    27 数组中出现次数超过一半的数字
    26 字符串的排列
    Python 实例2—购物车
    python_threading模块实现多线程详解(转)
    Ubuntu 16.04 安装Postman
  • 原文地址:https://www.cnblogs.com/chrysanthemum/p/11470569.html
Copyright © 2011-2022 走看看