½âÎöjsonÎļþ»ñÈ¡¹Ø¼üÐÅÏ¢
https://www.amap.com/service/cityList?version=2020121611Ôõô»ñÈ¡Õâ¸öÀïÃæµÄÊ¡»á³ÇÊÐ
лл ±¾Ìû×îºóÓÉ suchocolate ÓÚ 2021-1-1 16:37 ±à¼
import requests
def main():
url = 'https://www.amap.com/service/cityList'
headers = {'user-agent': 'firefox'}
r = requests.get(url, headers=headers)
json_data = r.json()['data']['cityData']['provinces']
for k, v in json_data.items():
if v['cities']:
print('Ê¡·Ý£º{0}, Ê¡»á³ÇÊУº{1}'.format(v['label'], v['cities']['name']))
else:
print('ֱϽÊÐ/ÌØÇø£º{}'.format(v['label']))
if __name__ == '__main__':
main()
Õâ¸öÍøÖ·ÊÇÈçºÎÀ´µÄ°¡ £¬ÏÖÔÚÒѾ¿´²»µ½³ÇÊÐÁË wp231957 ·¢±íÓÚ 2021-1-1 16:41
Õâ¸öÍøÖ·ÊÇÈçºÎÀ´µÄ°¡ £¬ÏÖÔÚÒѾ¿´²»µ½³ÇÊÐÁË
ºÃÏñÊǸöjson µãÒ»ÏÂÍøÖ·ÔÙË¢ÐÂ
¾ÍÓÐÁË ÎÒ¿´ÕâÀïÃæ³ÇÊкܶà,µ«ÊÇÔÚjsonÊý¾ÝÖÐûÓоßÌåÇø·ÖÊ¡»á³ÇÊеÄÇø±ð,Êý¾Ý¶¼²î²»¶à,ËùÒÔ»¹ÊÇÐèÒª×Ô¼º½¨Á¢Ò»¸öÁбí,°ÑÇø·ÖÌõ¼þ¼ÓÈë½øÈ¥,±ÈÈçÊ¡»á³ÇÊÐÃû³Æ,»òÕßÆäËûÌõ¼þ Ò²¿ÉÒÔÓÃÊ¡»á³ÇÊеÄareacodeÀ´Çø·Ö ÄãÏÈÓÃä¯ÀÀÆ÷Äõ½Êý¾Ý£¬·Åµ½JSON¸ñʽ¹¤¾ßÀï·ÖÎö£¬ÕÒÕÒÌØÕ÷¡£¶þÂ¥µÄ½â´ð¾Í·Ç³£ºÃ suchocolate ·¢±íÓÚ 2021-1-1 16:35
лл suchocolate ·¢±íÓÚ 2021-1-1 16:35
import requests
#»ñÈ¡³ÇÊÐÒÔ¼°¶ÔÓ¦µÄadcode
def get_city():
#È·¶¨url
base_url = 'https://www.amap.com/service/cityList?'
# ·¢ËÍÇëÇó
response = requests.get(base_url,headers=headers)
# print(response.text)
#½âÎöjsonÊý¾Ý
json_data = response.json()
# print(json_data) µÃµ½µÄjsonÊý¾Ý·Åµ½ÔÚÏßjson½âÎöÍøÕ¾ÖУ¬·½±ã·ÖÎö½á¹¹
#»ñÈ¡adcode
#ÈÈÃųÇÊÐ
city_adcode = []
for data in json_data['data']['cityData']['hotCitys']:
city_adcode.append((data['adcode'],data['name'],data['x'],data['y']))
#ÆäËû³ÇÊÐ
#for data in json_data['data']['cityData']['otherCitys']:
#city_adcode.append((data['adcode'],data['name']))
return city_adcode
print(city_adcode)
def get_weather(adcode,city_name,X,Y):
'''
»ñÈ¡³ÇÊÐÌìÆø
Query String Parameters
adcode:500000
'''
#»ù´¡url
base_url = 'https://www.amap.com/service/weather?adcode={}'.format(adcode)
response = requests.get(base_url, headers=headers)#·¢ËÍÇëÇ󣬻ñÈ¡ÏìÓ¦
json_data = response.json()#»ñÈ¡jsonÊý¾Ý
#ͨ¹ýµÃµ½µÄjsonÊý¾Ý£¬ÔÚÔÚÏß½âÎöÍøÕ¾ÖнâÎöºó£¬·ÖÎöÆä½á¹¹£¬ÕÒµ½Òª»ñÈ¡µÄÔÚÄÄÒ»¸ö×Öµä»òÕßÁбíÁбí
#·Ö²ãµÄÈ¡³öÀ´¼´¿É
if json_data['data']['result']=='true':
weather = json_data['data']['data']['forecast_data']['weather_name']#µ±Ç°ÌìÆø
#×î´óζÈ
max_temp = json_data['data']['data']['forecast_data']['max_temp']
#×îСζÈ
min_temp = json_data['data']['data']['forecast_data']['min_temp']
t_weather = json_data['data']['data']['forecast_data']['weather_name']#Ã÷ÌìÌìÆø
#×î´óζÈ
t_max_temp = json_data['data']['data']['forecast_data']['max_temp']
#×îСζÈ
t_min_temp = json_data['data']['data']['forecast_data']['min_temp']
# print(weather, max_temp, min_temp)
dic = {}
dic['³ÇÊÐ'] = city_name
dic['adcode'] = adcode
dic['X'] = X
dic['Y'] = Y
dic['ÌìÆø'] = weather
dic['ζÈ'] = '{}/{}¡æ'.format(min_temp,max_temp)
dic['Ã÷ÈÕÌìÆø'] = t_weather
dic['Ã÷ÈÕζÈ'] = '{}/{}¡æ'.format(t_min_temp,t_max_temp)
print(dic)
def main():
city_adcode = get_city()
# print(city_adcode)
#½«Ã¿¸ö³ÇÊеÄadcode´«¸øget_weather
#city_adcodeÓгÇÊкÍadcode
for i in city_adcode:
adcode = i
city_name = i
X = i
Y = i
get_weather(adcode,city_name,X,Y)
if __name__ == '__main__':
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537',
'X-Requested-With': 'XMLHttpRequest',
}
main()
´óÀÐ ÄÜÔÚÎÒÕâ¸ö´úÂëÉÏÃæ°Ñ³ÇÊиijÉÊ¡»á³ÇÊÐÂð ±¾Ìû×îºóÓÉ suchocolate ÓÚ 2021-1-5 19:50 ±à¼
˽¤Ï¤ê ·¢±íÓÚ 2021-1-5 13:57
´óÀÐ ÄÜÔÚÎÒÕâ¸ö´úÂëÉÏÃæ°Ñ³ÇÊиijÉÊ¡»á³ÇÊÐÂð
import requests
def main():
# ³ÇÊÐ×Öµä---> {'±±¾©': , 'ʯ¼Òׯ': , ...}
cities = dict()
url = 'https://www.amap.com/service/cityList'
headers = {'user-agent': 'Mobilla'}
r = requests.get(url, headers=headers)
json_data = r.json()['data']['cityData']['provinces']
for k, v in json_data.items():
# ÆÕͨʡ·Ý
if v['cities']:
t = v['cities']
cities] = , t['y'], t['adcode']]
# ֱϽÊлòÌØÇø
else:
cities] = , v['y'], k]
for k, v in cities.items():
url = 'https://www.amap.com/service/weather?adcode=' + v
# print(url)
r = requests.get(url, headers=headers)
json_data = r.json()['data']['data']
t_data = json_data['forecast_data']
m_data = json_data['forecast_data']
print(f'{k} ½ñÌì ÌìÆø:{t_data["weather_name"]}, ×î¸ßÎÂ{t_data["max_temp"]}£¬×îµÍÎÂ{t_data["min_temp"]}')
print(f' Ã÷ÈÕ ÌìÆø:{m_data["weather_name"]}, ×î¸ßÎÂ{m_data["max_temp"]}£¬×îµÍÎÂ{m_data["min_temp"]}')
print('=' * 100)
if __name__ == '__main__':
main() suchocolate ·¢±íÓÚ 2021-1-5 19:45
лл
Ò³:
[1]