資源簡介
python代碼,爬蟲爬取高德地圖POI數據,先注冊高德個人賬戶,獲取所需要的key,替代代碼中的key,然后更改省市,更改想要的數據類型,python運行即可得到xlse數據
代碼片段和文件信息
from?urllib.parse?import?quote
from?urllib?import?request
import?json
import?xlwt
#TODO?替換為上面申請的密鑰
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為城市下面的所有區,classes為多個分類名集合.?(中文名或者代碼都可以,代碼詳見高德地圖的POI分類編碼表)
cityname?=?‘周口‘
nanning_areas?=?[‘川匯區‘]
classes?=?[‘高中‘‘醫院‘‘公園‘‘小學‘]
#?根據城市名稱和分類關鍵字獲取poi數據
def?getpois(cityname?keywords):
????i?=?1
????poilist?=?[]
????while?True:??#?使用while循環不斷分頁獲取數據
????????result?=?getpoi_page(cityname?keywords?i)
????????result?=?json.loads(result)??#?將字符串轉換為json
????????if?result[‘count‘]?==?‘0‘:
????????????break
????????hand(poilist?result)
????????i?=?i?+?1
????return?poilist
#?數據寫入excel
def?write_to_excel(poilist?cityname?classfield):
????#?一個Workbook對象,這就相當于創建了一個Excel文件
????book?=?xlwt.Workbook(encoding=‘utf-8‘?style_compression=0)
????sheet?=?book.add_sheet(classfield?cell_overwrite_ok=True)
????#?第一行(列標題)
????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]
?
評論
共有 條評論