資源簡(jiǎn)介
python代碼,爬蟲爬取高德地圖POI數(shù)據(jù),先注冊(cè)高德個(gè)人賬戶,獲取所需要的key,替代代碼中的key,然后更改省市,更改想要的數(shù)據(jù)類型,python運(yùn)行即可得到xlse數(shù)據(jù)
代碼片段和文件信息
from?urllib.parse?import?quote
from?urllib?import?request
import?json
import?xlwt
#TODO?替換為上面申請(qǐng)的密鑰
amap_web_key?=?‘7eebc6ea2ce063c882284e1cc551de59‘
poi_search_url?=?“http://restapi.amap.com/v3/place/text“
poi_boundary_url?=?“https://ditu.amap.com/detail/get/detail“
#from?transCoordinateSystem?import?gcj02_to_wgs84
#TODO?cityname為需要爬取的POI所屬的城市名,nanning_areas為城市下面的所有區(qū),classes為多個(gè)分類名集合.?(中文名或者代碼都可以,代碼詳見高德地圖的POI分類編碼表)
cityname?=?‘周口‘
nanning_areas?=?[‘川匯區(qū)‘]
classes?=?[‘高中‘‘醫(yī)院‘‘公園‘‘小學(xué)‘]
#?根據(jù)城市名稱和分類關(guān)鍵字獲取poi數(shù)據(jù)
def?getpois(cityname?keywords):
????i?=?1
????poilist?=?[]
????while?True:??#?使用while循環(huán)不斷分頁(yè)獲取數(shù)據(jù)
????????result?=?getpoi_page(cityname?keywords?i)
????????result?=?json.loads(result)??#?將字符串轉(zhuǎn)換為json
????????if?result[‘count‘]?==?‘0‘:
????????????break
????????hand(poilist?result)
????????i?=?i?+?1
????return?poilist
#?數(shù)據(jù)寫入excel
def?write_to_excel(poilist?cityname?classfield):
????#?一個(gè)Workbook對(duì)象,這就相當(dāng)于創(chuàng)建了一個(gè)Excel文件
????book?=?xlwt.Workbook(encoding=‘utf-8‘?style_compression=0)
????sheet?=?book.add_sheet(classfield?cell_overwrite_ok=True)
????#?第一行(列標(biāo)題)
????sheet.write(0?0?‘x‘)
????sheet.write(0?1?‘y‘)
????sheet.write(0?2?‘count‘)
????sheet.write(0?3?‘name‘)
????for?i?in?range(len(poilist)):
????????location?=?poilist[i][‘location‘]
????????name?=?poilist[i][‘name‘]
????????lng?=?str(location).split(““)[0]
????????lat?=?str(location).split(““)[1]
?
評(píng)論
共有 條評(píng)論