欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2019年01月16日 16:34:36   作者:我是醬油誰(shuí)要打我  
這篇文章主要為大家詳細(xì)介紹了python調(diào)用百度地圖WEB服務(wù)API獲取地點(diǎn)對(duì)應(yīng)坐標(biāo)值,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

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

最終結(jié)果是寫(xiě)入了txt文件,所以需要在循環(huán)遇到錯(cuò)誤的時(shí)候?qū)懭雽?duì)應(yīng)的可識(shí)別的值(看到這個(gè)值就知道這個(gè)結(jié)果是錯(cuò)誤的,可以寫(xiě)對(duì)應(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/地址對(duì)應(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'] # 開(kāi)發(fā)者應(yīng)用密鑰
cod = r'&ret_coordtype=bd09ll' # 坐標(biāo)類(lèi)型(設(shè)置為百度坐標(biāo))
machine_data = csv.reader(open(origin_path, 'r', encoding='utf-8')) # 讀取原始文件數(shù)據(jù)
n = 0
akn = 0
column_names = '設(shè)備序列號(hào) 取點(diǎn)方式1 準(zhǔn)確度1 網(wǎng)點(diǎn)緯度 網(wǎng)點(diǎn)經(jīng)度 網(wǎng)點(diǎn)名稱(chēng) 取點(diǎn)方式2 準(zhǔn)確度2 安裝地址緯度 安裝地址經(jīng)度 安裝地址 取點(diǎn) 準(zhǔn)確度 最佳緯度 最佳經(jīng)度 安裝方式 最佳地址'
with open(new_path, 'a', encoding='utf-8') as f: # 把變量名寫(xiě)入新文件
 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è)備序列號(hào)
   wd = str(addr[3])  # 網(wǎng)點(diǎn)名稱(chēng)
   anz = str(addr[4])  # 安裝地址
   anz_type = str(addr[5]) # 安裝類(lèi)型
   add1 = province + city + wd
   add2 = province + city + anz
   if akn < len(AK): # AK配額還沒(méi)用完時(shí)
    n += 1
    aknd = AK[akn] # 第akn個(gè)秘鑰是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) # 請(qǐng)求網(wǎng)址響應(yīng)
    soup = BeautifulSoup(response.content, 'html.parser') # 解析網(wǎng)頁(yè)內(nèi)容
    response.close() # 獲取內(nèi)容后關(guān)閉網(wǎng)頁(yè)(防止被遠(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)打點(diǎn),可靠性高;0為模糊打點(diǎn),準(zhǔn)確性低
     confidence1 = dictinfo['result']['confidence'] # 可信度,描述打點(diǎn)準(zhǔn)確度,大于80表示誤差小于100m
     geocode1 = str(precise1) + ' ' + str(confidence1) + ' ' + str(lat1) + ' ' + str(lng1) + ' ' + add1
    elif status == 302 or status == 210: # 302 配額超限,限制訪問(wèn);210 IP驗(yàn)證未通過(guò),則使用下一個(gè)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) # 請(qǐng)求網(wǎng)址響應(yīng)
    soup2 = BeautifulSoup(response2.content, 'html.parser') # 解析內(nèi)容
    response2.close() # 獲取內(nèi)容后關(guān)閉網(wǎng)頁(yè)(防止被遠(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)打點(diǎn),可靠性高;0為模糊打點(diǎn),準(zhǔn)確性低
     confidence2 = dictinfo2['result']['confidence'] # 可信度,描述打點(diǎn)準(zhǔn)確度,大于80表示誤差小于100m
     geocode2 = str(precise2) + ' ' + str(confidence2) + ' ' + str(lat2) + ' ' + str(lng2) + ' ' + add2
    elif status2 == 302 or status2 == 210: # 配額超限,限制訪問(wèn);IP驗(yàn)證未通過(guò)
     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)點(diǎn)'
     elif precise1 == 0 and precise2 == 0:
      geocode3 = str(precise1) + ' ' + str(confidence1) + ' ' + str(lat1) + ' ' + str(lng1) + ' ' + anz_type + ' 網(wǎng)點(diǎn)'
     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 # 配額不足中斷整個(gè)循環(huán)
  print('已完成')
 except: # 發(fā)生錯(cuò)誤時(shí)執(zhí)行以下代碼塊
  print('未知錯(cuò)誤')
  time.sleep(5)
  with open(new_path, 'a', encoding='utf-8') as f:
   f.write('未知錯(cuò)誤')
   f.write('\n')
   f.close()
  continue # 發(fā)生未知錯(cuò)誤跳過(guò)該次循環(huán)
 print('程序已停止')
 break

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論