国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python調用百度地圖WEB服務API獲取地點對應坐標值

2020-02-16 00:42:19
字體:
來源:轉載
供稿:網友

本篇博客介紹如何使用Python調用百度地圖WEB服務API獲取地點對應坐標值,現有一系列結構化地址數據(如:北京市海淀區上地十街十號),目的是獲取對應坐標值。

百度地圖開發者平臺路線規劃使用說明網址

最終結果是寫入了txt文件,所以需要在循環遇到錯誤的時候寫入對應的可識別的值(看到這個值就知道這個結果是錯誤的,可以寫對應數量的NA或者0值),方便后續分析。

# -*- coding: utf-8 -*-"""Created on Fri Aug 15 10:06:16 2018@author: zjpPython3.6.6""" # 加載必要的包 import csvimport jsonimport timeimport requestsfrom 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'] # 開發者應用密鑰cod = r'&ret_coordtype=bd09ll' # 坐標類型(設置為百度坐標)machine_data = csv.reader(open(origin_path, 'r', encoding='utf-8')) # 讀取原始文件數據n = 0akn = 0column_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: # 循環爬取每一條數據   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狀態碼為0表示服務器響應成功,本次循環爬取數據成功     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 # 配額不足中斷整個循環  print('已完成') except: # 發生錯誤時執行以下代碼塊  print('未知錯誤')  time.sleep(5)  with open(new_path, 'a', encoding='utf-8') as f:   f.write('未知錯誤')   f.write('/n')   f.close()  continue # 發生未知錯誤跳過該次循環 print('程序已停止') break            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 杭锦后旗| 沂源县| 桂东县| 泊头市| 马山县| 永胜县| 法库县| 卓尼县| 辽宁省| 伊金霍洛旗| 鲜城| 巩留县| 府谷县| 新昌县| 叙永县| 娄底市| 桐乡市| 报价| 资兴市| 昌宁县| 汝南县| 阿拉善左旗| 运城市| 溆浦县| 屏山县| 襄樊市| 阿拉尔市| 扶风县| 夹江县| 马尔康县| 灵石县| 望江县| 凭祥市| 行唐县| 吉木乃县| 稻城县| 中西区| 平塘县| 蒙自县| 渝中区| 探索|