爬蟲(chóng)練習(xí)案例(爬取高德地圖中所有城市的天氣信息)

代碼如下:

"""

需求:爬取高德地圖上面所有城市的天氣信息

"""

# 導(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)行效果如下:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容