网络编程
位置:首页>> 网络编程>> Python编程>> Python 通过调用接口获取公交信息的实例

Python 通过调用接口获取公交信息的实例

作者:SnowRomance  发布时间:2023-06-17 21:16:46 

标签:Python,接口,公交

如下所示:


# -*- coding: utf-8 -*-
import sys, urllib, urllib2, json

city=urllib.quote(sys.argv[1]);

url = 'http://apis.baidu.com/xiaota/bus_lines/buses_lines?city=%s&bus=%s&direction=%s'%(city,sys.argv[2],sys.argv[3])

print url

req = urllib2.Request(url)

req.add_header("apikey", "2f5da4b87cbd02a5f8be1189db99b6a8")

resp = urllib2.urlopen(req)
content = resp.read()
if(content):
    print(content)

print "\n"
busStation = json.loads(content)
print busStation.keys()
print busStation['data'].keys()
print busStation['data']['stations']

for bus in busStation['data']['stations']:
        print bus['stateName']

来源:https://blog.csdn.net/lyj1101066558/article/details/51302248

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com