
代碼如下:
"""
需求:爬取高德地圖上面所有城市的天氣信息
"""
# 導(dǎo)入需要的模塊
import requests, json
# 定制請(qǐng)求頭
headers = {
'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'
}
# 定義獲取adcode的url
adcode_url ='https://ditu.amap.com/service/cityList?version='
# 發(fā)送請(qǐng)求
adc_response = requests.get(url=adcode_url,headers=headers)
adc_result = adc_response.json()
# 獲取cityBYletter數(shù)據(jù)
cityByletter = adc_result['data']['cityByLetter']
# 獲取每個(gè)城市的天氣信息的url
weather_url ='https://ditu.amap.com/service/weather?adcode={}'
# 循環(huán)遍歷獲取城市列表
for city_listin cityByletter.values():
# 循環(huán)出每一個(gè)城市的字典
? ? for cityin city_list:
# 定義一個(gè)空字典,用于保存每個(gè)城市的數(shù)據(jù)
? ? ? ? city_info = {}
# 通過(guò)鍵值對(duì)獲取對(duì)應(yīng)內(nèi)容
? ? ? ? # 獲取城市的adcode
? ? ? ? adcode = city['adcode']
# 獲取城市的name
? ? ? ? city_info['city_name'] = city['name']
# 將天氣的url拼接好,發(fā)送請(qǐng)求
? ? ? ? weather_response = requests.get(url=weather_url.format(adcode),headers=headers)
# 使用json.loads()將json轉(zhuǎn)換成python類(lèi)型,或使用weather_response.json()也可以-->weather_data = json.loads(weather_response.text)
? ? ? ? weather_data = weather_response.json()
# 開(kāi)始提取數(shù)據(jù)
? ? ? ? zuiwai_data = weather_data['data']
# 獲取result值
? ? ? ? result = zuiwai_data['result']
# 判斷resutl的值是否為true,如果是true,再進(jìn)行提取
? ? ? ? if result =='true':
# 獲取forecast_data數(shù)據(jù)
? ? ? ? ? ? forecast_data = zuiwai_data['data'][0]['forecast_data'][0]
# 獲取天氣名稱(chēng)
? ? ? ? ? ? city_info['weather_name'] = forecast_data['weather_name']
# 獲取最低氣溫
? ? ? ? ? ? city_info['min_temp'] = forecast_data['min_temp']
# 獲取最高氣溫
? ? ? ? ? ? city_info['max_temp'] = forecast_data['max_temp']
# 寫(xiě)入txt文件
? ? ? ? ? ? with open('gaode.txt','a',encoding='utf-8')as fp:
fp.write(str(city_info)+'\n')
print(city_info)



運(yùn)行效果如下:
