爬虫--爬取高德地图全国天气预报

import requests,json
#定义初步的url
base_url="https://www.amap.com/service/cityList?version=201951410"
#发起请求
response=requests.get(base_url)
json_data=response.json()#接受json格式的数据
# print(json_data)
#转换字符串便于分析
# print(json.dumps(json_data))
#获取所有的城市和adcode
json_data2=json_data.get('data')
# print(json_data2)
json_data3=json_data2.get('cityByLetter')
# print(json_data3)
#将所有城市和adcode组成字典放入一个列表中
adcode_data_list=[]
for j,k in json_data3.items():
    # print(k)
    for x in k:
        adcode_data_list.append(x)
        #     print(adcode_data_list)
#获取城市个数,350个
# print(len(adcode_data_list))#350个
for count,city_dict in enumerate(adcode_data_list):
    count+=1
    adcode=city_dict.get('adcode')
    # print(adcode)
#拼接完整的天气
    base_url="https://www.amap.com/service/weather?adcode="+ adcode
    response=requests.get(base_url)
    data_json=response.json()
    # print(data_json)
    # print(json.dumps(data_json))
    try:
        weather=data_json.get("data").get('data')[0].get("live").get('weather_name')
    except TypeError as e:
        print(e)
    finally:
        city_dict['adcode']=adcode
        city_dict['name']=city_dict.get('name')
        city_dict['weather']=weather
        print(count,city_dict)
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值