溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python調(diào)用百度地圖WEB服務(wù)API獲取地點對應(yīng)坐標(biāo)值

發(fā)布時間:2020-08-30 08:20:03 來源:腳本之家 閱讀:371 作者:我是醬油誰要打我 欄目:開發(fā)技術(shù)

本篇博客介紹如何使用Python調(diào)用百度地圖WEB服務(wù)API獲取地點對應(yīng)坐標(biāo)值,現(xiàn)有一系列結(jié)構(gòu)化地址數(shù)據(jù)(如:北京市海淀區(qū)上地十街十號),目的是獲取對應(yīng)坐標(biāo)值。

百度地圖開發(fā)者平臺路線規(guī)劃使用說明網(wǎng)址

最終結(jié)果是寫入了txt文件,所以需要在循環(huán)遇到錯誤的時候?qū)懭雽?yīng)的可識別的值(看到這個值就知道這個結(jié)果是錯誤的,可以寫對應(yīng)數(shù)量的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' # 原始數(shù)據(jù)文件路徑
new_path = 'E://GetRoute/HuaNan/地址對應(yīng)坐標(biāo).txt'  # 爬取數(shù)據(jù)文件保存路徑
 
 
url_geocode = r'http://api.map.baidu.com/geocoder/v2/?' # 百度地圖api網(wǎng)址
AK = ['oFCSeioUzdN5NfzSlBBXqBEfXgp26mGM', 'Akqk5xjbSGzy1WC1IUF04K2CQWGtOFNv', 'HCdq1Ry35rwgVQwjAXqAEQGzWNY7pi1h',
  'GtOZERwlG0PynPwFrBYaF9wWcAGxvaw8', 'iRKkZehZimIWdGoxfjlbtLrYb0VVgVaD', 'gG0KIBhAGpAVvaRUlwFjmOtsTKGRK2tf',
  'CSsyosiklqyYUDNnBP0BR63fa9BzCHFf', 'mq4TZshHveVqML3icCC6AWnS25rbjYBz', 'rBYetA6WQNOlXtQWInz8ckRE0iCDsUjB',
  'QUshHD8KUAk8y9gLwDhQ6RyOgQxEB8VD', '7Ict6oZmpAYYXMjha2Tk5g4ENTCYwx03'] # 開發(fā)者應(yīng)用密鑰
cod = r'&ret_coordtype=bd09ll' # 坐標(biāo)類型(設(shè)置為百度坐標(biāo))
machine_data = csv.reader(open(origin_path, 'r', encoding='utf-8')) # 讀取原始文件數(shù)據(jù)
n = 0
akn = 0
column_names = '設(shè)備序列號 取點方式1 準(zhǔn)確度1 網(wǎng)點緯度 網(wǎng)點經(jīng)度 網(wǎng)點名稱 取點方式2 準(zhǔn)確度2 安裝地址緯度 安裝地址經(jīng)度 安裝地址 取點 準(zhǔn)確度 最佳緯度 最佳經(jīng)度 安裝方式 最佳地址'
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)爬取每一條數(shù)據(jù)
   province = str(addr[0]) # 省份
   city = str(addr[1]) # 城市
   mac = str(addr[2])  # 設(shè)備序列號
   wd = str(addr[3])  # 網(wǎng)點名稱
   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網(wǎng)址
    response = requests.get(url=tar_url) # 請求網(wǎng)址響應(yīng)
    soup = BeautifulSoup(response.content, 'html.parser') # 解析網(wǎng)頁內(nèi)容
    response.close() # 獲取內(nèi)容后關(guān)閉網(wǎng)頁(防止被遠(yuǎn)程主機(jī)認(rèn)定為攻擊行為)
    dictinfo = json.loads(str(soup)) # json數(shù)據(jù)轉(zhuǎn)dict數(shù)據(jù)
    status = dictinfo['status']
    print(status)
    if status == 0: # status狀態(tài)碼為0表示服務(wù)器響應(yīng)成功,本次循環(huán)爬取數(shù)據(jù)成功
     lng1 = round(dictinfo['result']['location']['lng'], 8) # 經(jīng)度保留8位數(shù)
     lat1 = round(dictinfo['result']['location']['lat'], 8) # 緯度保留8位數(shù)
     precise1 = dictinfo['result']['precise'] # 1為精準(zhǔn)打點,可靠性高;0為模糊打點,準(zhǔn)確性低
     confidence1 = dictinfo['result']['confidence'] # 可信度,描述打點準(zhǔn)確度,大于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) # 請求網(wǎng)址響應(yīng)
    soup2 = BeautifulSoup(response2.content, 'html.parser') # 解析內(nèi)容
    response2.close() # 獲取內(nèi)容后關(guān)閉網(wǎng)頁(防止被遠(yuǎn)程主機(jī)認(rèn)定為攻擊行為)
    dictinfo2 = json.loads(str(soup2)) # json轉(zhuǎn)dict
    status2 = dictinfo2['status']
    print(status2)
    if status2 == 0:
     lng2 = round(dictinfo2['result']['location']['lng'], 8) # 經(jīng)度保留8位數(shù)
     lat2 = round(dictinfo2['result']['location']['lat'], 8) # 緯度保留8位數(shù)
     precise2 = dictinfo2['result']['precise'] # 1為精準(zhǔn)打點,可靠性高;0為模糊打點,準(zhǔn)確性低
     confidence2 = dictinfo2['result']['confidence'] # 可信度,描述打點準(zhǔn)確度,大于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 + ' 網(wǎng)點'
     elif precise1 == 0 and precise2 == 0:
      geocode3 = str(precise1) + ' ' + str(confidence1) + ' ' + str(lat1) + ' ' + str(lng1) + ' ' + anz_type + ' 網(wǎng)點'
     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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI