import vk_api import pickle import json vk_session = vk_api.VkApi('login', 'Pass') # Π»ΠΎΠ³ΠΈΠ½ ΠΈ ΠΏΠ°ΡΠΎΠ»Ρ vk_session.auth() vk = vk_session.get_api() def main(): y = vk.groups.getMembers(group_id='your_group_id', fields='contacts') # Id Π³ΡΡΠΏΠΏΡ ΠΈ Π½ΠΎΠΌΠ΅ΡΠ° ΡΠ΅Π»Π΅ΡΠΎΠ½ΠΎΠ² ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»Π΅ΠΉ ΡΡΠΎΠΉ Π³ΡΡΠΏΠΏΡ data = y json_str = json.dumps(data) resp = json.loads(json_str) print(resp) print(resp['items']) data_items = str(resp['items']) with open("ΡΠ°ΠΉΠ».txt", "w") as file: print(data_items, file=file) if __name__ == '__main__': main() So I get a JSON of the form:
{'items': [{'first_name': 'ΠΠ»Ρ', 'id': 111111, 'last_name': 'Π‘ΡΡΠ΅Π½ΠΊΠΎ'}, {'first_name': 'ΠΠ΅ΠΎΡΠ³ΠΈΠΉ', 'id': 222222, 'last_name': 'ΠΠΎΠ»ΠΎΡΠΎΠ²'}, {'first_name': 'ΠΠ°ΠΊΡΠΈΠΌ', 'id': 333333, 'home_phone':+79909999999 '', 'last_name': 'Π’ΡΠΏΠΈΡΠ΅Π½ΠΊΠΎΠ²'} which in turn is a dict - is the dictionary, I understand correctly? Tell me please.
And how can I export this JSON to csv? All my attempts ended only in that I put coutn in one line, and on the second, the whole 'answer' of JSON (in one line) with dictionary keys and LINKS TO DATA (how to draw this data) ?. Here, the difficulty is that the data obtained is not monotonous, someone has contact details and someone does not, and the home_number / mobile_number fields may be missing and I already anticipate what this will result in when exporting, at least there will be problems when creating columns in the table, tell me how to solve this problem correctly?
In desperation, I decided to unload all the data into a txt file, on output I got a txt file of type:
{'first_name': 'ΠΠ»Ρ', 'id': 111111, 'last_name': 'Π‘ΡΡΠ΅Π½ΠΊΠΎ'}, {'first_name': 'ΠΠ΅ΠΎΡΠ³ΠΈΠΉ', 'id': 222222, 'last_name': 'ΠΠΎΠ»ΠΎΡΠΎΠ²'}, {'first_name': 'ΠΠ°ΠΊΡΠΈΠΌ', 'id': 333333, 'home_phone':+79909999999 '', 'last_name': 'Π’ΡΠΏΠΈΡΠ΅Π½ΠΊΠΎΠ²'} can it be possible to sort / fraud it somehow with a script to get the desired csv?