python調用百度地圖WEB服務API獲取地點對應坐標值
更新時間:2019年01月16日 16:34:36 作者:我是醬油誰要打我
這篇文章主要為大家詳細介紹了python調用百度地圖WEB服務API獲取地點對應坐標值,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本篇博客介紹如何使用Python調用百度地圖WEB服務API獲取地點對應坐標值,現有一系列結構化地址數據(如:北京市海淀區(qū)上地十街十號),目的是獲取對應坐標值。
百度地圖開發(fā)者平臺路線規(guī)劃使用說明網址
最終結果是寫入了txt文件,所以需要在循環(huán)遇到錯誤的時候寫入對應的可識別的值(看到這個值就知道這個結果是錯誤的,可以寫對應數量的NA或者0值),方便后續(xù)分析。
# -*- coding: utf-8 -*- """ Created on Fri Aug 15 10:06:16 2018 @author: zjp Python3.6.6 """ # 加載必要的包 import csv import json import time import requests from bs4 import BeautifulSoup origin_path = 'E://GetRoute/HuaNan/中文地址.csv' # 原始數據文件路徑 new_path = 'E://GetRoute/HuaNan/地址對應坐標.txt' # 爬取數據文件保存路徑 url_geocode = r'http://api.map.baidu.com/geocoder/v2/?' # 百度地圖api網址 AK = ['oFCSeioUzdN5NfzSlBBXqBEfXgp26mGM', 'Akqk5xjbSGzy1WC1IUF04K2CQWGtOFNv', 'HCdq1Ry35rwgVQwjAXqAEQGzWNY7pi1h', 'GtOZERwlG0PynPwFrBYaF9wWcAGxvaw8', 'iRKkZehZimIWdGoxfjlbtLrYb0VVgVaD', 'gG0KIBhAGpAVvaRUlwFjmOtsTKGRK2tf', 'CSsyosiklqyYUDNnBP0BR63fa9BzCHFf', 'mq4TZshHveVqML3icCC6AWnS25rbjYBz', 'rBYetA6WQNOlXtQWInz8ckRE0iCDsUjB', 'QUshHD8KUAk8y9gLwDhQ6RyOgQxEB8VD', '7Ict6oZmpAYYXMjha2Tk5g4ENTCYwx03'] # 開發(fā)者應用密鑰 cod = r'&ret_coordtype=bd09ll' # 坐標類型(設置為百度坐標) machine_data = csv.reader(open(origin_path, 'r', encoding='utf-8')) # 讀取原始文件數據 n = 0 akn = 0 column_names = '設備序列號 取點方式1 準確度1 網點緯度 網點經度 網點名稱 取點方式2 準確度2 安裝地址緯度 安裝地址經度 安裝地址 取點 準確度 最佳緯度 最佳經度 安裝方式 最佳地址' with open(new_path, 'a', encoding='utf-8') as f: # 把變量名寫入新文件 f.write(column_names) f.write('\n') f.close() while True: try: for addr in machine_data: # 循環(huán)爬取每一條數據 province = str(addr[0]) # 省份 city = str(addr[1]) # 城市 mac = str(addr[2]) # 設備序列號 wd = str(addr[3]) # 網點名稱 anz = str(addr[4]) # 安裝地址 anz_type = str(addr[5]) # 安裝類型 add1 = province + city + wd add2 = province + city + anz if akn < len(AK): # AK配額還沒用完時 n += 1 aknd = AK[akn] # 第akn個秘鑰是aknd ak = r'&output=json&ak=' + aknd address1 = r'address=' + add1 tar_url = url_geocode + address1 + ak + cod # 最終url網址 response = requests.get(url=tar_url) # 請求網址響應 soup = BeautifulSoup(response.content, 'html.parser') # 解析網頁內容 response.close() # 獲取內容后關閉網頁(防止被遠程主機認定為攻擊行為) dictinfo = json.loads(str(soup)) # json數據轉dict數據 status = dictinfo['status'] print(status) if status == 0: # status狀態(tài)碼為0表示服務器響應成功,本次循環(huán)爬取數據成功 lng1 = round(dictinfo['result']['location']['lng'], 8) # 經度保留8位數 lat1 = round(dictinfo['result']['location']['lat'], 8) # 緯度保留8位數 precise1 = dictinfo['result']['precise'] # 1為精準打點,可靠性高;0為模糊打點,準確性低 confidence1 = dictinfo['result']['confidence'] # 可信度,描述打點準確度,大于80表示誤差小于100m geocode1 = str(precise1) + ' ' + str(confidence1) + ' ' + str(lat1) + ' ' + str(lng1) + ' ' + add1 elif status == 302 or status == 210: # 302 配額超限,限制訪問;210 IP驗證未通過,則使用下一個Ak akn += 1 lat1 = 'break' lng1 = 'break' precise1 = 0 confidence1 = 0 geocode1 = '0 0 break break ' + add1 else: lat1 = 'na' lng1 = 'na' precise1 = 0 confidence1 = 0 geocode1 = '0 0 na na ' + add1 address2 = r'address=' + add2 tar_url2 = url_geocode + address2 + ak + cod # 總的url response2 = requests.get(url=tar_url2) # 請求網址響應 soup2 = BeautifulSoup(response2.content, 'html.parser') # 解析內容 response2.close() # 獲取內容后關閉網頁(防止被遠程主機認定為攻擊行為) dictinfo2 = json.loads(str(soup2)) # json轉dict status2 = dictinfo2['status'] print(status2) if status2 == 0: lng2 = round(dictinfo2['result']['location']['lng'], 8) # 經度保留8位數 lat2 = round(dictinfo2['result']['location']['lat'], 8) # 緯度保留8位數 precise2 = dictinfo2['result']['precise'] # 1為精準打點,可靠性高;0為模糊打點,準確性低 confidence2 = dictinfo2['result']['confidence'] # 可信度,描述打點準確度,大于80表示誤差小于100m geocode2 = str(precise2) + ' ' + str(confidence2) + ' ' + str(lat2) + ' ' + str(lng2) + ' ' + add2 elif status2 == 302 or status2 == 210: # 配額超限,限制訪問;IP驗證未通過 akn += 1 precise2 = 0 confidence2 = 0 lat2 = 'break' lng2 = 'break' geocode2 = '0 0 break break ' + add2 else: lat2 = 'na' lng2 = 'na' precise2 = 0 confidence2 = 0 geocode2 = '0 0 na na ' + add2 if anz_type == '在行': if precise1 == 1: geocode3 = str(precise1) + ' ' + str(confidence1) + ' ' + str(lat1) + ' ' + str(lng1) + ' ' + anz_type + ' 網點' elif precise1 == 0 and precise2 == 0: geocode3 = str(precise1) + ' ' + str(confidence1) + ' ' + str(lat1) + ' ' + str(lng1) + ' ' + anz_type + ' 網點' else: geocode3 = str(precise2) + ' ' + str(confidence2) + ' ' + str(lat2) + ' ' + str(lng2) + ' ' + anz_type + ' 安裝地址' else: geocode3 = str(precise2) + ' ' + str(confidence2) + ' ' + str(lat2) + ' ' + str(lng2) + ' ' + anz_type + ' 安裝地址' geocode = mac + ' ' + geocode1 + ' ' + geocode2 + ' ' + geocode3 with open(new_path, 'a', encoding='utf-8') as f: f.write(geocode) f.write('\n') f.close() print('good' + str(n)) else: print('配額不足!') break # 配額不足中斷整個循環(huán) print('已完成') except: # 發(fā)生錯誤時執(zhí)行以下代碼塊 print('未知錯誤') time.sleep(5) with open(new_path, 'a', encoding='utf-8') as f: f.write('未知錯誤') f.write('\n') f.close() continue # 發(fā)生未知錯誤跳過該次循環(huán) print('程序已停止') break
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
python使用paramiko執(zhí)行服務器腳本并拿到實時結果
這篇文章主要介紹了python使用paramiko執(zhí)行服務器腳本并拿到實時結果,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12Python中識別圖片/滑塊驗證碼準確率極高的ddddocr庫詳解
驗證碼的種類有很多,它是常用的一種反爬手段,包括:圖片驗證碼,滑塊驗證碼,等一些常見的驗證碼場景。這里推薦一個簡單實用的識別驗證碼的庫?ddddocr?(帶帶弟弟ocr)庫,希望大家喜歡2023-02-02Python使用lxml模塊和Requests模塊抓取HTML頁面的教程
用Pyhton自帶的urllib或urllib2模塊抓取網頁或許有些陳詞濫調了,今天我們就來玩兒些新鮮的,來看Python使用lxml模塊和Requests模塊抓取HTML頁面的教程:2016-05-05