高德地图爬取所有城市天气数据

import requests
import json


class GaoDeMap:
    def __init__(self):
        self.run()

    def run(self):
        base_url = "https://www.amap.com/service/cityList?version=201941111"
        response = requests.get(base_url)
        json_data = response.json()
        # print(json_data)
        cityByLetter = json_data["data"]["cityByLetter"]
        # print(cityByLetter)
        # cityByLetter.values() 就是adcode和name组成的很多字典
        count = 0
        dict = {}
        for i in cityByLetter.values():
            # print(i)
            for j in i:
                # print(j["adcode"], j["name"])
                count += 1
                # 爬取天气
                base_url2 = "https://www.amap.com/service/weather?adcode=" + j["adcode"]
                response2 = requests.get(base_url2)
                json_data2 = response2.json()
                # 获取天气
                weather_name = json_data2["data"]["data"][0]["live"]["weather_name"]
                # print(weather_name)
                # 将城市名name adcode 和天气组成一个字典
                dict["city"] = j["name"]
                dict["adcode"] = j["adcode"]
                dict["weather_name"] = weather_name
                print(count, dict)


if __name__ == '__main__':
    GaoDeMap()



你可能感兴趣的:(python爬虫)