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))
爬虫-高德地图
最新推荐文章于 2024-10-12 22:39:44 发布