Help to modify the python script for parsing members of Vkontakte group, please. Now it saves the result as club[id группы].txt . I want to add another group name to the name of the saved file. As far as I understand, for this you need to make an additional request , in the json answer there will be the string name: "КИРИШИ CLUB" . Tell me how to isolate it and add to the file name?

 import requests import time as t import csv from datetime import datetime, date, time, timedelta token = 'xxxx' def write_txt(members_list, owner_id): a = open('club'+str(owner_id)+'.txt', 'w') for i in members_list: a.write(str(i)+'\n') #писать мож a.close() def get_members_list_id(owner_id): print('Начал работать в:',datetime.strftime(datetime.now(), "%H:%M:%S")) members_list = [] #изначально пустой список участников #первый запрос на 25000, чтобы получить первые 25000 и количество участников группы r = requests.post('https://api.vk.com/method/execute.Shmakov_getClub_members?group_id='+ str(owner_id)+'&offset='+str(0)+'&count='+str(25000)+'&access_token='+token).json()['response'] members_count = r[0] #количество участников print('Участников:',members_count) members_list.extend(r[1]) #вносим первые 25000 ID if members_count > 25000: print('Больше 25k участников. Запускаем цикл') for offset in range(25000, members_count, 25000): count = offset + 25000 r = requests.post('https://api.vk.com/method/execute.Shmakov_getClub_members?group_id='+ str(owner_id)+'&offset='+str(offset)+'&count='+str(count)+'&access_token='+token).json()['response'] members_list.extend(r[1]) #вносим все последующие ID пачками по 25000 ID #t.sleep(.35) #задержки между запросом --- ВАЖНО: если будут возникать проблемы - расскоментировать print('Цербер закончил сбор ID') else: print('>25k участников. Закончили сбор ID') print(len(members_list)) write_txt(members_list, owner_id) #записываем по 25000 ID print('Данные успешно записаны') print('Остановка:',datetime.strftime(datetime.now(), "%H:%M:%S")) get_ipython().magic('time get_members_list_id(12345)') 
  • And what execute.Shmakov_getClub_members ? Is this some kind of feature of your application? - gil9red
  • 3
    Why not use VC API for python? Why do you do all the requests? - Pavel Durmanov

1 answer 1

 import requests req = requests.post(r'https://api.vk.com/method/groups.getById?group_id={0}'.format(owner_id)) print(req.json()['response'][0]['name']) 

Result:

 КИРИШИ CLUB 

To substitute this in the file name, for example, you can do this:

 import requests owner_id = 12345 req = requests.post(r'https://api.vk.com/method/groups.getById?group_id={0}'.format(owner_id)).json()['response'][0]['name'] file_name = open('{0}_{1}.txt'.format(req, owner_id), 'w') 

At the output we get the file:

 КИРИШИ CLUB_12345.txt 

UODATE

To remove all unnecessary characters from the group name:

 In [25]: import re In [26]: pub_name = 'VK "", "", "!?,@#$%^&*_+|+\/:;[]{}<>") Public' In [27]: re.sub(r'[\W+_]', '', pub_name) Out[27]: 'VKPublic'