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

yipeiwu_com7年前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']

以上这篇Python 通过调用接口获取公交信息的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python 字典 setdefault()和get()方法比较详解

python 字典 setdefault()和get()方法比较详解

dict.setdefault(key, default=None) --> 有key获取值,否则设置 key:default,并返回default,default默认值为None...

pandas全表查询定位某个值所在行列的方法

如下所示: # create a dataframe with an integer feature and a categorical string feature demo_df...

python写入并获取剪切板内容的实例

写桌面程序或有些特殊操作的,经常需要访问剪切板。python有专用的模块,可以很方便简单的操作剪切板 如下: #coding:utf-8 import win32clipboard...

Python实现的最近最少使用算法

本文实例讲述了Python实现的最近最少使用算法。分享给大家供大家参考。具体如下: # lrucache.py -- a simple LRU (Least-Recently-Use...

Python检测网络延迟的代码

本文讲述了Python检测网络延迟的代码。分享给大家供大家参考,具体如下: #!/usr/bin/env python # coding: utf-8 # coding: cp9...