如何通过python从foursquare获取给定用户id的用户签入
嗨,我写这段代码是为了得到一个用户签入计数。我设法为自己获得一个simplejson反序列化数据对象。我想知道如何访问数据对象中的单个元素,因为当前um得到了类似的东西如何通过python从foursquare获取给定用户id的用户签入,python,foursquare,Python,Foursquare,嗨,我写这段代码是为了得到一个用户签入计数。我设法为自己获得一个simplejson反序列化数据对象。我想知道如何访问数据对象中的单个元素,因为当前um得到了类似的东西 class FourSquareTestHandler(SecurePageHandler): def get(self): logging.info('***********************') logging.info('*****GET****') logging.info('***
class FourSquareTestHandler(SecurePageHandler):
def get(self):
logging.info('***********************')
logging.info('*****GET****')
logging.info('***********************')
try:
request =\
urllib2.Request('https://api.foursquare.com/v2/users/self/checkins?oauth_token=LAF1W3VMSI0DGQYIBTNIYVIZG4VE2RFGESM45LDJRSQHFTK3&v=20120123')
data = simplejson.load(urllib2.urlopen(request))
logging.info('=========================')
logging.info('*********Success*********')
logging.info('=========================')
logging.info(data)
logging.info('======================================')
except Exception:
logging.info('**********EXCEPTION*********')
pass
context = {}
return self.render_response('foursquaretest.html', **context)
您的
数据
是一个python字典对象
里面有嵌套的字典和列表
例如
{
u 'notifications': [{
u 'item': {
u 'unreadCount': 0
},
u 'type': u 'notificationTray'
}],
u 'meta': {
u 'code': 200
},
u 'response': {
u 'checkins': {
u 'count': 6,
u 'items': [{
u 'venue': {
u 'verified': False,
u 'name': u 'ODEL Warehouse',
u 'contact': {},
u 'location': {
u 'city': u 'Colombo',
u 'country': u 'Sri Lanka',
u 'postalCode': u '00600',
u 'state': u 'Western Province',
u 'address': u 'Highlevel Rd',
u 'lat': 6.8754399960498391,
u 'lng': 79.881239696240982
},
u 'stats': {
u 'tipCount': 2,
u 'checkinsCount': 76,
u 'usersCount': 25
},
u 'id': u '4d217410b69c6dcbf1787995',
u 'categories': []
},
u 'comments': {
u 'count': 0,
u 'items': []
},
u 'photos': {
u 'count': 0,
u 'items': []
},
u 'timeZone': u 'Asia/Colombo',
u 'type': u 'checkin',
u 'id': u '4f1d1940e4b0d1a657597d73',
u 'createdAt': 1327307072
}, {
u 'venue': {
u 'verified': False,
u 'name': u 'Chesmi Consolidated',
u 'contact': {},
u 'location': {
u 'lat': 6.8783062620154825,
u 'city': u 'colombo 5',
u 'lng': 79.879610192565167,
u 'country': u 'Sri Lanka',
u 'address': u '21, Siebel Avenue,'
},
u 'stats': {
u 'tipCount': 0,
u 'checkinsCount': 2,
u 'usersCount': 2
},
u 'id': u '4ed4cab6e5fa7b1a923b1e1e',
u 'categories': []
},
u 'comments': {
u 'count': 0,
u 'items': []
},
u 'photos': {
u 'count': 0,
u 'items': []
},
u 'timeZone': u 'Asia/Colombo',
u 'type': u 'checkin',
u 'id': u '4f1d1920e4b0e6badd487a53',
u 'createdAt': 1327307040
}, {
u 'venue': {
u 'verified': False,
u 'name': u 'Commercial Bank',
u 'contact': {},
u 'location': {
u 'city': u 'Colombo 5',
u 'country': u 'Sri Lanka',
u 'postalCode': u '00500',
u 'state': u 'Western Province',
u 'address': u 'High Level Rd',
u 'lat': 6.8776848396168955,
u 'lng': 79.879610192565167
},
u 'stats': {
u 'tipCount': 0,
u 'checkinsCount': 42,
u 'usersCount': 9
},
u 'id': u '4c85bdb1d4e237047aaf8588',
u 'categories': [{
u 'pluralName': u 'Banks',
u 'primary': True,
u 'name': u 'Bank',
u 'shortName': u 'Bank / Financial',
u 'id': u '4bf58dd8d48988d10a951735',
u 'icon': {
u 'prefix': u 'https://foursquare.com/img/categories/shops/financial_',
u 'name': u '.png',
u 'sizes': [32, 44, 64, 88, 256]
}
}]
},
u 'comments': {
u 'count': 0,
u 'items': []
},
u 'photos': {
u 'count': 0,
u 'items': []
},
u 'timeZone': u 'Asia/Colombo',
u 'type': u 'checkin',
u 'id': u '4f1d18f5e4b0e6badd485953',
u 'createdAt': 1327306997
}, {
u 'venue': {
u 'verified': False,
u 'name': u 'Siebel Avenue Bus Stop 138',
u 'contact': {},
u 'location': {
u 'lat': 6.8775550799999996,
u 'country': u 'Sri Lanka',
u 'lng': 79.879341159999996
},
u 'stats': {
u 'tipCount': 3,
u 'checkinsCount': 76,
u 'usersCount': 15
},
u 'id': u '4e5c53b345dd045aab4d7dfa',
u 'categories': [{
u 'pluralName': u 'Bus Stations',
u 'primary': True,
u 'name': u 'Bus Station',
u 'shortName': u 'Bus Station',
u 'id': u '4bf58dd8d48988d1fe931735',
u 'icon': {
u 'prefix': u 'https://foursquare.com/img/categories/travel/busstation_',
u 'name': u '.png',
u 'sizes': [32, 44, 64, 88, 256]
}
}]
},
u 'comments': {
u 'count': 0,
u 'items': []
},
u 'photos': {
u 'count': 0,
u 'items': []
},
u 'timeZone': u 'Asia/Colombo',
u 'type': u 'checkin',
u 'id': u '4f1d18e1e4b0e2eeed04eff2',
u 'createdAt': 1327306977
}, {
u 'venue': {
u 'verified': False,
u 'name': u 'Visni',
u 'contact': {},
u 'location': {
u 'lat': 6.8785901069641113,
u 'country': u 'Sri Lanka',
u 'lng': 79.879791259765625
},
u 'stats': {
u 'tipCount': 1,
u 'checkinsCount': 8,
u 'usersCount': 1
},
u 'id': u '4eb75312f5b94bd85c9fa375',
u 'categories': [{
u 'pluralName': u 'Tech Startups',
u 'primary': True,
u 'name': u 'Tech Startup',
u 'shortName': u 'Tech Startup',
u 'id': u '4bf58dd8d48988d125941735',
u 'icon': {
u 'prefix': u 'https://foursquare.com/img/categories/shops/technology_',
u 'name': u '.png',
u 'sizes': [32, 44, 64, 88, 256]
}
}]
},
u 'comments': {
u 'count': 0,
u 'items': []
},
u 'photos': {
u 'count': 0,
u 'items': []
},
u 'timeZone': u 'Asia/Colombo',
u 'type': u 'checkin',
u 'id': u '4f1d18cde4b0850c85e124d1',
u 'createdAt': 1327306957
}, {
u 'venue': {
u 'verified': False,
u 'name': u 'Calcey Technologies',
u 'contact': {
u 'phone': u '0112827560',
u 'formattedPhone': u '011 2 827560'
},
u 'location': {
u 'city': u 'Colombo 5',
u 'country': u 'Sri Lanka',
u 'postalCode': u '00500',
u 'state': u 'Sri Lanka',
u 'crossStreet': u 'Highlevel Rd',
u 'address': u '21 Siebel Ave',
u 'lat': 6.8782598000000004,
u 'lng': 79.879707733333333
},
u 'stats': {
u 'tipCount': 0,
u 'checkinsCount': 324,
u 'usersCount': 14
},
u 'id': u '4c43efd62d3ec9b60e3f2eae',
u 'categories': [{
u 'pluralName': u 'Tech Startups',
u 'primary': True,
u 'name': u 'Tech Startup',
u 'shortName': u 'Tech Startup',
u 'id': u '4bf58dd8d48988d125941735',
u 'icon': {
u 'prefix': u 'https://foursquare.com/img/categories/shops/technology_',
u 'name': u '.png',
u 'sizes': [32, 44, 64, 88, 256]
}
}]
},
u 'comments': {
u 'count': 0,
u 'items': []
},
u 'photos': {
u 'count': 0,
u 'items': []
},
u 'timeZone': u 'Asia/Colombo',
u 'type': u 'checkin',
u 'id': u '4f1d18a3e4b0a6271ab6c175',
u 'createdAt': 1327306915
}]
}
}
}
这显示有6个(签入)条目。因此,要获取第一个(第0个列表项)签入的id
要获得第一条评论(内容)
与其他字段类似。您的
数据是一个python字典对象
里面有嵌套的字典和列表
例如
{
u 'notifications': [{
u 'item': {
u 'unreadCount': 0
},
u 'type': u 'notificationTray'
}],
u 'meta': {
u 'code': 200
},
u 'response': {
u 'checkins': {
u 'count': 6,
u 'items': [{
u 'venue': {
u 'verified': False,
u 'name': u 'ODEL Warehouse',
u 'contact': {},
u 'location': {
u 'city': u 'Colombo',
u 'country': u 'Sri Lanka',
u 'postalCode': u '00600',
u 'state': u 'Western Province',
u 'address': u 'Highlevel Rd',
u 'lat': 6.8754399960498391,
u 'lng': 79.881239696240982
},
u 'stats': {
u 'tipCount': 2,
u 'checkinsCount': 76,
u 'usersCount': 25
},
u 'id': u '4d217410b69c6dcbf1787995',
u 'categories': []
},
u 'comments': {
u 'count': 0,
u 'items': []
},
u 'photos': {
u 'count': 0,
u 'items': []
},
u 'timeZone': u 'Asia/Colombo',
u 'type': u 'checkin',
u 'id': u '4f1d1940e4b0d1a657597d73',
u 'createdAt': 1327307072
}, {
u 'venue': {
u 'verified': False,
u 'name': u 'Chesmi Consolidated',
u 'contact': {},
u 'location': {
u 'lat': 6.8783062620154825,
u 'city': u 'colombo 5',
u 'lng': 79.879610192565167,
u 'country': u 'Sri Lanka',
u 'address': u '21, Siebel Avenue,'
},
u 'stats': {
u 'tipCount': 0,
u 'checkinsCount': 2,
u 'usersCount': 2
},
u 'id': u '4ed4cab6e5fa7b1a923b1e1e',
u 'categories': []
},
u 'comments': {
u 'count': 0,
u 'items': []
},
u 'photos': {
u 'count': 0,
u 'items': []
},
u 'timeZone': u 'Asia/Colombo',
u 'type': u 'checkin',
u 'id': u '4f1d1920e4b0e6badd487a53',
u 'createdAt': 1327307040
}, {
u 'venue': {
u 'verified': False,
u 'name': u 'Commercial Bank',
u 'contact': {},
u 'location': {
u 'city': u 'Colombo 5',
u 'country': u 'Sri Lanka',
u 'postalCode': u '00500',
u 'state': u 'Western Province',
u 'address': u 'High Level Rd',
u 'lat': 6.8776848396168955,
u 'lng': 79.879610192565167
},
u 'stats': {
u 'tipCount': 0,
u 'checkinsCount': 42,
u 'usersCount': 9
},
u 'id': u '4c85bdb1d4e237047aaf8588',
u 'categories': [{
u 'pluralName': u 'Banks',
u 'primary': True,
u 'name': u 'Bank',
u 'shortName': u 'Bank / Financial',
u 'id': u '4bf58dd8d48988d10a951735',
u 'icon': {
u 'prefix': u 'https://foursquare.com/img/categories/shops/financial_',
u 'name': u '.png',
u 'sizes': [32, 44, 64, 88, 256]
}
}]
},
u 'comments': {
u 'count': 0,
u 'items': []
},
u 'photos': {
u 'count': 0,
u 'items': []
},
u 'timeZone': u 'Asia/Colombo',
u 'type': u 'checkin',
u 'id': u '4f1d18f5e4b0e6badd485953',
u 'createdAt': 1327306997
}, {
u 'venue': {
u 'verified': False,
u 'name': u 'Siebel Avenue Bus Stop 138',
u 'contact': {},
u 'location': {
u 'lat': 6.8775550799999996,
u 'country': u 'Sri Lanka',
u 'lng': 79.879341159999996
},
u 'stats': {
u 'tipCount': 3,
u 'checkinsCount': 76,
u 'usersCount': 15
},
u 'id': u '4e5c53b345dd045aab4d7dfa',
u 'categories': [{
u 'pluralName': u 'Bus Stations',
u 'primary': True,
u 'name': u 'Bus Station',
u 'shortName': u 'Bus Station',
u 'id': u '4bf58dd8d48988d1fe931735',
u 'icon': {
u 'prefix': u 'https://foursquare.com/img/categories/travel/busstation_',
u 'name': u '.png',
u 'sizes': [32, 44, 64, 88, 256]
}
}]
},
u 'comments': {
u 'count': 0,
u 'items': []
},
u 'photos': {
u 'count': 0,
u 'items': []
},
u 'timeZone': u 'Asia/Colombo',
u 'type': u 'checkin',
u 'id': u '4f1d18e1e4b0e2eeed04eff2',
u 'createdAt': 1327306977
}, {
u 'venue': {
u 'verified': False,
u 'name': u 'Visni',
u 'contact': {},
u 'location': {
u 'lat': 6.8785901069641113,
u 'country': u 'Sri Lanka',
u 'lng': 79.879791259765625
},
u 'stats': {
u 'tipCount': 1,
u 'checkinsCount': 8,
u 'usersCount': 1
},
u 'id': u '4eb75312f5b94bd85c9fa375',
u 'categories': [{
u 'pluralName': u 'Tech Startups',
u 'primary': True,
u 'name': u 'Tech Startup',
u 'shortName': u 'Tech Startup',
u 'id': u '4bf58dd8d48988d125941735',
u 'icon': {
u 'prefix': u 'https://foursquare.com/img/categories/shops/technology_',
u 'name': u '.png',
u 'sizes': [32, 44, 64, 88, 256]
}
}]
},
u 'comments': {
u 'count': 0,
u 'items': []
},
u 'photos': {
u 'count': 0,
u 'items': []
},
u 'timeZone': u 'Asia/Colombo',
u 'type': u 'checkin',
u 'id': u '4f1d18cde4b0850c85e124d1',
u 'createdAt': 1327306957
}, {
u 'venue': {
u 'verified': False,
u 'name': u 'Calcey Technologies',
u 'contact': {
u 'phone': u '0112827560',
u 'formattedPhone': u '011 2 827560'
},
u 'location': {
u 'city': u 'Colombo 5',
u 'country': u 'Sri Lanka',
u 'postalCode': u '00500',
u 'state': u 'Sri Lanka',
u 'crossStreet': u 'Highlevel Rd',
u 'address': u '21 Siebel Ave',
u 'lat': 6.8782598000000004,
u 'lng': 79.879707733333333
},
u 'stats': {
u 'tipCount': 0,
u 'checkinsCount': 324,
u 'usersCount': 14
},
u 'id': u '4c43efd62d3ec9b60e3f2eae',
u 'categories': [{
u 'pluralName': u 'Tech Startups',
u 'primary': True,
u 'name': u 'Tech Startup',
u 'shortName': u 'Tech Startup',
u 'id': u '4bf58dd8d48988d125941735',
u 'icon': {
u 'prefix': u 'https://foursquare.com/img/categories/shops/technology_',
u 'name': u '.png',
u 'sizes': [32, 44, 64, 88, 256]
}
}]
},
u 'comments': {
u 'count': 0,
u 'items': []
},
u 'photos': {
u 'count': 0,
u 'items': []
},
u 'timeZone': u 'Asia/Colombo',
u 'type': u 'checkin',
u 'id': u '4f1d18a3e4b0a6271ab6c175',
u 'createdAt': 1327306915
}]
}
}
}
这显示有6个(签入)条目。因此,要获取第一个(第0个列表项)签入的id
要获得第一条评论(内容)
其他领域也类似。@Hugo,编辑使问题变得非常不同。提问者没有问这个问题,他是否可以把他的问题格式化成这样;)。与其编辑问题,不如将格式作为一个单独的答案。@Hugo,编辑使问题大不相同。提问者没有问这个问题,他是否可以把他的问题格式化成这样;)。与其编辑问题,不如将格式设置作为单独的答案。
>>> data['response']['checkins']['items'][0]['id']
u'4f1d1940e4b0d1a657597d73'
>>> data['response']['checkins']['items'][0]['comments']['items'][0]