• 爬取高德天气所有城市的天气


    1、打开网站:https://www.amap.com/

    2、按F12进入开发者界面,如下图

    3、点击Network--XHR--刷新,如下图

    4、找到存放天气的文件

     

    如何获取图中adcode,因为adcode为获取每个城市天气的标识。

    ①复制adcode在上个图中的左下方文件中的Preview找到对应城市的adcode

    ②上图文件数据格式是json的格式,可以打开网站:json.cn  可以有效的查看

    5、分析完浏览器数据结构后,进行代码的编写

    import requests
    import json
    
    # 查找adcode
    
    # 爬取城市adcode的url地址
    base_url = 'https://www.amap.com/service/cityList?version=201922117'
    # 发起请求
    response_city = requests.get(base_url)
    # 将服务器响应回来的数据转换成json格式
    json_data = response_city.json()
    
    json_data2 = json_data['data']['cityByLetter']
    
    # 存储城市的信息
    city_list = []
    for key,value in json_data2.items():
        for city in value:
            city_list.append(city)
    
    # 遍历城市信息 获取所有城市的天气
    
    for i, city in enumerate(city_list):
        i += 1
        # 将获取到的adcode存到adcode变量中
        adcode = city['adcode']
        city_name = city['name']
    
        # 定义爬取天气的url地址
        weather_url = f'https://www.amap.com/service/weather?adcode={adcode}'
        response_weather = requests.get(weather_url)
        json_data3 = response_weather.json()
    
        # 获取json_data3字典中的天气值
        weather = json_data3.get('data').get('data')[0].get('live').get('weather_name')
        limit = json_data3.get('data').get('data')[0].get('live').get('temperature')
    
        print(i, city_name, weather, limit+'C')
    代码 
  • 相关阅读:
    学习进度八
    ”“口袋“app的nabcd
    学习进度7
    软工人3月7日学习记录
    软工人3月6日学习
    软工人3月5日学习
    开课博客
    android入门之Android环境配置
    大二寒假学习
    python链接数据库并创建表
  • 原文地址:https://www.cnblogs.com/renshaoqi/p/10420299.html
Copyright © 2020-2023  润新知