生活随笔
收集整理的這篇文章主要介紹了
python从高德api获取公交线路规划信息
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
從高德api獲取公交線路規(guī)劃信息類似于爬蟲(chóng),由于本人不會(huì)爬蟲(chóng),因此簡(jiǎn)單說(shuō)說(shuō)如何實(shí)現(xiàn)
直接上代碼
import os
import requests
import math
import json
import numpy
as np
import csv
import pandas
as pd
def request_url_get(url
):""" 請(qǐng)求url方法get方法 """try:r
= requests
.get
(url
=url
, timeout
=30)if r
.status_code
== 200:return r
.text
return Noneexcept RequestException
:print('請(qǐng)求url返回錯(cuò)誤異常')return Nonedef parse_json(content_json
):""" 解析json函數(shù) """result_json
= json
.loads
(content_json
)return result_json
def request_api(url
):""" 請(qǐng)求高德api 解析json """result
= request_url_get
(url
)result_json
= parse_json
(result
)return result_json
def run():""" 修改參數(shù)區(qū) """origin
= '116.481028,39.989643'destination
= '116.434446,39.90816'city
= 'beijing'index_url
= f'https://restapi.amap.com/v3/direction/transit/integrated?origin={origin}&destination={destination}&' \
f'city={city}&key=“你自己申請(qǐng)的key”'""" 不許動(dòng)區(qū) """index_result
= request_api
(index_url
)df
= pd
.DataFrame
(index_result
['route']['transits'][0]['segments'])df
= df
['bus'][1]['buslines']df
= pd
.DataFrame
(df
)df
.to_csv
('test.csv', sep
=',', index
=False, encoding
="utf_8_sig")if __name__
!= "main":try:run
()print("運(yùn)行完了")except:print("啊啊啊,出錯(cuò)了")
總結(jié)
以上是生活随笔為你收集整理的python从高德api获取公交线路规划信息的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。