爬虫-高德地图

import requests,random,json
from  fake_useragent import UserAgent
ua = UserAgent()

# proxies_list = [{'http': 'http://36.25.243.251:80', 'https': 'http://121.69.26.14:8080'},
#            {'http': 'http://36.25.243.251:80',  'https': 'http://218.76.253.201:61408'},
#            {'http': 'http://36.25.243.251:80', 'https': 'http://42.238.90.249:9999'},
#            ]
# proxies = random.choice(proxies_list)
url = 'https://www.amap.com/service/cityList'
headers = {'User-Agent':ua.random}
# response = requests.get(url,headers=headers,proxies=proxies).json()
response = requests.get(url,headers=headers).json()
# response = requests.get(url,headers=headers)
citybyletter = response['data']['cityByLetter']
# with open('gaode.json','w', encoding='utf-8')as fp:
#     fp.write(response.text)
key = '433c672f21925a5c50fcb632d907f9ea'
lis = []
for k,v in citybyletter.items():
#     print(k)
#     print(v)

    for i in v:
        dic = {}
        city_name = i.get('name')
        city_adcode = i.get('adcode')
        weather_url = 'https://restapi.amap.com/v3/weather/weatherInfo?city={}&key={}'.format(city_adcode,key)
        weather_response = requests.get(weather_url,headers=headers).json()
        weather = weather_response['lives'][0]
        # report_time = weather['reporttime']
        # temperature = weather['temperature']
        try:
            weather_name = weather['weather']
        except:
            weather_name = 'null'
        dic[city_name] = weather_name
        lis.append(dic)
with open('gaode2.json','a+',encoding='utf-8') as f:
    json.dump(lis,f,ensure_ascii=False)

# with open('gaode.json','a+',encoding='utf-8') as f:
#     f.write(str(lis))







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值