@Rbugs

Как парсить json полученный из API VK в Python?

Добрый день, начал изучать Python, а как известно лучшее обучение это практика, придумал себе задачку. Собрать список аккаунтов инстаграм друзей в вконтакте.
Зарегистрировал приложение, получил токен, сделал запрос метод friends.get:

import vk, json, ast
from datetime import datetime
id_user_vk=СВОЙIDVK
session = vk.AuthSession(access_token='ТОКЕН', scope='wall')
vk_api = vk.API(session, v='5.87')
r = vk_api.users.get(user_id=id_user_vk, fields='online, last_seen, connections')
s = vk_api.friends.get(user_id=id_user_vk, order='hints', fields='connections')
s1=s['items']
with open('friends_id_'+str(id_user_vk)+'_'+str(datetime.now())+'.json', 'w') as f:
    json.dump(s, f, ensure_ascii=False)

получил json ответ:
{"count": 104, "items": [{"id": 62535, "first_name": "Наталья", "last_name": "Панас-Гольдфайн", "online": 0}, {"id": 114002, "first_name": "Алексей", "last_name": "Крайнев", "online": 0}, {"id": 342955, "first_name": "Елена", "last_name": "Эркенова", "online": 1}, {"id": 343342, "first_name": "Оксана", "last_name": "Тарасова", "online": 0}, {"id": 363480, "first_name": "Василий", "last_name": "Незабудко", "online": 0}, {"id": 520989, "first_name": "Наталия", "last_name": "Яшина", "online": 0}, {"id": 608219, "first_name": "Марина", "last_name": "Загорская", "online": 1}, {"id": 702245, "first_name": "Юля", "last_name": "Багышова", "facebook": "+115158715688218", "facebook_name": "Юлия Багышова", "instagram": "yulia_bagishova", "online": 0}, {"id": 848620, "first_name": "Natasha", "last_name": "Scott", "online": 0}, {"id": 901451, "first_name": "Маргарита", "last_name": "Beyer", "online": 0}, {"id": 939508, "first_name": "Анжелина", "last_name": "Карелина", "online": 0}, {"id": 1010521, "first_name": "Юлия", "last_name": "Блохина", "online": 0}, {"id": 1197989, "first_name": "Юлия", "last_name": "Вершило", "online": 0}, {"id": 1273230, "first_name": "Ирина", "last_name": "Белова", "online": 0}, {"id": 1321756, "first_name": "Елена", "last_name": "Булохова", "online": 0}, {"id": 1335304, "first_name": "Евгения", "last_name": "Владимирова", "online": 1}, {"id": 1503979, "first_name": "Andrey", "last_name": "Andreev", "online": 0}, {"id": 1565731, "first_name": "Надежда", "last_name": "Трифонова", "online": 0}, {"id": 1634009, "first_name": "Надежда", "last_name": "Жайлмисова", "online": 0}, {"id": 1670255, "first_name": "Константин", "last_name": "Бирюлин", "online": 0}, {"id": 1802350, "first_name": "Елена", "last_name": "Васильева", "online": 0}, {"id": 1873345, "first_name": "Евгений", "last_name": "Бирюлин", "skype": "jacksperow3", "twitter": "EvgeniyBirylin", "instagram": "evgeniy_birylin", "online": 0}, {"id": 2162412, "first_name": "Ким", "last_name": "Ульяна", "online": 0}, {"id": 2210943, "first_name": "Иван", "last_name": "Чеклин", "online": 0}, {"id": 2314282, "first_name": "Сергей", "last_name": "Бирюлин", "online": 0}, {"id": 2428091, "first_name": "Георгий", "last_name": "Самойлов", "online": 0}, {"id": 2588000, "first_name": "Елена", "last_name": "Фетисова", "online": 0}, {"id": 3312706, "first_name": "Maxim", "last_name": "Borisov", "online": 0}, {"id": 3692352, "first_name": "Марина", "last_name": "Бирюлина", "online": 0}, {"id": 3855711, "first_name": "Валентина", "last_name": "Гусарова", "online": 0}, {"id": 3858820, "first_name": "Александр", "last_name": "Фетисов", "online": 0}, {"id": 3918588, "first_name": "Вовчик", "last_name": "Бирюлин", "skype": "vovchick08", "online": 0}, {"id": 3919666, "first_name": "Алиса", "last_name": "Цуканова", "online": 0}, {"id": 3942608, "first_name": "Сергей", "last_name": "Самойлов", "online": 0}, {"id": 3995356, "first_name": "Наташа", "last_name": "Нефедова", "online": 0}, {"id": 4108102, "first_name": "Анна", "last_name": "Kachalova", "online": 0}, {"id": 4333913, "first_name": "Ирина", "last_name": "Парлак", "online": 0}, {"id": 4435881, "first_name": "Татьяна", "last_name": "Веселова", "online": 0}, {"id": 4473067, "first_name": "Павел", "last_name": "Кудрявцев", "online": 0}, {"id": 4794879, "first_name": "Виктория", "last_name": "Становова", "online": 0}, {"id": 5028492, "first_name": "Руслан", "last_name": "Василёнок", "online": 0}, {"id": 5316995, "first_name": "Надежда", "last_name": "Никишина", "online": 0}, {"id": 5634966, "first_name": "Юлия", "last_name": "Романенко", "online": 0}, {"id": 6239108, "first_name": "Нина", "last_name": "Становова", "online": 0}, {"id": 7015564, "first_name": "Елена", "last_name": "Федорова", "online": 0}, {"id": 7398164, "first_name": "Нина", "last_name": "Шехтер", "online": 0}, {"id": 7884431, "first_name": "Сергей", "last_name": "Никишин", "online": 0}, {"id": 8826978, "first_name": "Екатерина", "last_name": "Копыльцова", "online": 0}, {"id": 10227015, "first_name": "Ирина", "last_name": "Вивсяная", "online": 1}, {"id": 11757374, "first_name": "Стас", "last_name": "Семёнов", "online": 0}, {"id": 13849744, "first_name": "Ксения", "last_name": "Шкурина", "online": 0}, {"id": 14148808, "first_name": "Ирина", "last_name": "Субботина", "online": 1}, {"id": 14198816, "first_name": "Катерина", "last_name": "Ежикова", "online": 0}, {"id": 14216146, "first_name": "Наталья", "last_name": "Жильцова", "online": 0}, {"id": 15124818, "first_name": "Екатерина", "last_name": "Осипова", "online": 0}, {"id": 15291559, "first_name": "Татьяна", "last_name": "Науменко", "online": 0}, {"id": 16101094, "first_name": "Ольга", "last_name": "Полищук", "online": 0}, {"id": 16691713, "first_name": "Иман", "last_name": "Эдальбиева", "online": 0}, {"id": 19211599, "first_name": "Ярослав", "last_name": "Чебров", "instagram": "chebrov", "online": 1}, {"id": 19944406, "first_name": "Артем", "last_name": "Белов", "online": 0}, {"id": 25661354, "first_name": "Ольга", "last_name": "Исаева(Кемкина)", "skype": "olgaisaeva61", "online": 0}, {"id": 27043765, "first_name": "Лидия", "last_name": "Нехорошкова", "online": 0}, {"id": 28123546, "first_name": "Елена", "last_name": "Кондратьева", "online": 0}, {"id": 28514787, "first_name": "Катя", "last_name": "Приходько", "online": 0}, {"id": 30657288, "first_name": "Владимир", "last_name": "Михальчишин", "online": 0}, {"id": 37144895, "first_name": "Анна", "last_name": "Мальцева", "online": 0}, {"id": 40020649, "first_name": "Александра", "last_name": "Белякова", "online": 0}, {"id": 48148455, "first_name": "Ирина", "last_name": "Маслова", "skype": "Irishechka_irisska", "online": 0}, {"id": 56291888, "first_name": "Аня", "last_name": "Никишина", "online": 0}, {"id": 65694786, "first_name": "Оксана", "last_name": "Ругаева", "online": 0}, {"id": 80144554, "first_name": "Роман", "last_name": "Зубко", "online": 0}, {"id": 82896104, "first_name": "Виктория", "last_name": "Ильина", "online": 0}, {"id": 85435684, "first_name": "Юлиана", "last_name": "Щербенко", "skype": "Liliy_333", "online": 0}, {"id": 106154506, "first_name": "Наталья", "last_name": "Мироевская", "online": 0}, {"id": 109391896, "first_name": "Ирина", "last_name": "Стеценко", "online": 0}, {"id": 112610500, "first_name": "Владимир", "last_name": "Никишин", "online": 0}, {"id": 134242351, "first_name": "Сергей", "last_name": "Жданов", "online": 1}, {"id": 140296022, "first_name": "Наталья", "last_name": "Конова", "skype": "angel110211", "online": 0}, {"id": 140953334, "first_name": "Татьяна", "last_name": "Семенова", "online": 0}, {"id": 140994273, "first_name": "Танюта", "last_name": "Горбачева", "online": 1}, {"id": 145257226, "first_name": "Оксана", "last_name": "Петрова", "online": 0}, {"id": 147545598, "first_name": "Ирина", "last_name": "Налётова", "online": 0}, {"id": 158539251, "first_name": "Елена", "last_name": "Калякина", "online": 0}, {"id": 160041079, "first_name": "Татьяна", "last_name": "Федорова", "skype": "orhidea485", "online": 0}, {"id": 161671138, "first_name": "Ömer", "last_name": "Yavuz", "online": 0}, {"id": 162457229, "first_name": "Елена", "last_name": "Сергеева", "online": 0}, {"id": 165847348, "first_name": "Hakan", "last_name": "Alparslan", "online": 0}, {"id": 176652848, "first_name": "Валентин", "last_name": "Кузнецов", "online": 0}, {"id": 182395492, "first_name": "Татьяна", "last_name": "Лобаскова", "online": 1}, {"id": 191135942, "first_name": "Евгений", "last_name": "Жданов", "online": 0}, {"id": 194165886, "first_name": "Pino", "last_name": "Ricotta", "online": 0}, {"id": 200193297, "first_name": "Ekaterina", "last_name": "Trofimova", "online": 0}, {"id": 204975656, "first_name": "Роза", "last_name": "Бирюлина", "online": 0}, {"id": 213096790, "first_name": "Марина", "last_name": "Демидова", "online": 0}, {"id": 227550811, "first_name": "Виктория", "last_name": "Спицына", "online": 0}, {"id": 271969528, "first_name": "Anton", "last_name": "Sizov", "skype": "krasafcheg08", "instagram": "t999ea", "online": 0}, {"id": 362181017, "first_name": "Светлана", "last_name": "Царева", "online": 0}, {"id": 378140344, "first_name": "Лиза", "last_name": "Ветрова", "online": 0}, {"id": 382404587, "first_name": "Елена", "last_name": "Каморникова", "online": 0}, {"id": 397465145, "first_name": "Сергей", "last_name": "Бирюлин", "online": 0}, {"id": 453606323, "first_name": "Галина", "last_name": "Ларина", "online": 0}, {"id": 480174744, "first_name": "Galina", "last_name": "Demchenko", "online": 0}, {"id": 482912332, "first_name": "Елена", "last_name": "Каморникова", "online": 0}, {"id": 501789991, "first_name": "Галина", "last_name": "Бирюлина", "online": 0}]}

а как из него получить только список аккаунтов instagram не понимаю... с циклами у меня слабовато)
в ответ приходит словарь, я из него делаю список s1=s['items'] с вложенными словарями, но ключ 'instagram' далеко не у всех есть.
  • Вопрос задан
  • 845 просмотров
Решения вопроса 1
@nukler
местный юродивый
[ print(i) for i in my_frendz.get("items") if i.get("skype")]
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы