U
U
uirriu2021-08-24 00:53:24
Python
uirriu, 2021-08-24 00:53:24

How to write data to a file in the given order?

I'm trying to make sure that data from VKontakte is written to the file in the following form: first the post, followed by the first comment, all the replies to this first comment, then the next comment and all the replies to the second comment, when all the comments are over, the next post and etc. But something doesn't work. Please help me understand what is wrong and how to do it.

import csv

import requests

token = 'token'
version = 5.131
owner_id = '-174228153'
post_id = 0
comment_id = 0
count = 10
i = 0


# функция записи данных в файл
def file_writer(all_post, all_comments, all_undercom, i):
    with open('parsing.csv', 'a', encoding='utf-8') as file:
        a_pen = csv.writer(file)
        a_pen.writerow(['id', 'text', 'likes', 'reposts', 'type', 'parents_stak', 'owner_id'])
        i = i
        for m, k, z in zip(all_post, all_comments, all_undercom):
            a_pen.writerow((all_post[i]['id'], all_post[i]['text'], all_post[i]['likes']['count'],
                            all_post[i]['reposts']['count'], 'post', '-', all_post[i]['owner_id']))
            j = 0
            for k in all_comments:

                if j < len(all_comments):
                    a_pen.writerow([all_comments[j]['id'], all_comments[j]['text'], '-', '-', 'comment',
                                    all_comments[j]['post_id'], all_comments[j]['owner_id']])
                    j += 1
                    t = 0
                    for z in all_undercom:
                        if t < len(all_undercom):
                            a_pen.writerow((all_undercom[t]['id'], all_undercom[t]['text'], '-', '-', 'subcomment',
                                            all_undercom[t]['id'], all_undercom[t]['owner_id']))
                            t += 1
                        else:
                            pass
                else:
                    pass




# получение всех постов
def get_posts(token, owner_id, count):
    token = token
    version = 5.131
    owner_id = owner_id
    count = count
    offset = 0
    all_posts = []

    while offset < 100:
        response = requests.get('https://api.vk.com/method/wall.get',
                                params={
                                    'access_token': token,
                                    'v': version,
                                    'owner_id': owner_id,
                                    'count': count,
                                    'offset': offset
                                })
        data = response.json()['response']['items']
        offset += 100
        all_posts.extend(data)
    return all_posts


# Получение id текущего поста
def get_post_id(all_post, i):
    for post in all_post:
        post_id = all_post[i]['id']
        return post_id


# получение всех коментариев под постом
def get_comments(token, owner_id, post_id, count):
    token = token
    version = 5.131
    owner_id = owner_id 
    post_id = post_id
    count = count
    offset = 0
    all_comments = []

    while offset < 100:
        response = requests.get('https://api.vk.com/method/wall.getComments',
                                params={
                                    'access_token': token,
                                    'v': version,
                                    'owner_id': owner_id,
                                    'post_id': post_id,
                                    'count': count,
                                    'offset': offset
                                })
        data = response.json()['response']['items']
        offset += 100
        all_comments.extend(data)
    return all_comments


# получение id текущего комментария
def get_comment_id(all_comments, i):
    comment_id = None
    for coment in all_comments:
        if i < len(all_comments):
            comment_id = all_comments[i]['id']
        else:
            pass
        return comment_id


# получение ветки подкоментариев
def get_vetcom(token, owner_id, post_id, comment_id, count):
    token = token
    version = 5.131
    owner_id = owner_id
    post_id = post_id
    comment_id = comment_id
    count = count
    offset = 0
    all_comments_vet = []

    while offset < 100:
        response = requests.get('https://api.vk.com/method/wall.getComments',
                                params={
                                    'access_token': token,
                                    'v': version,
                                    'owner_id': owner_id,
                                    'post_id': post_id,
                                    'comment_id': comment_id,
                                    'count': count,
                                    'offset': offset
                                })
        data = response.json()['response']['items']
        offset += 100
        all_comments_vet.extend(data)
    return all_comments_vet


all_post = get_posts(token, owner_id, 10)  # получили список постов

while i < len(all_post):
    post_id = get_post_id(all_post, i)  # получили id текущего поста
    all_comments = get_comments(token, owner_id, post_id, 10)  # получили список коментариев под этим постом
    comment_id = get_comment_id(all_comments, i)  # получили id n-го комментария из списка комментариев
    all_undercom = get_vetcom(token, owner_id, post_id, comment_id, 10)  # получили список подкоментариев этого коментария
    file_writer(all_post, all_comments, all_undercom, i)
    i += 1

    print(all_post,all_comments,all_undercom)

print(1)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-08-24
@uirriu

for m, k, z in zip(all_post, all_comments, all_undercom):
Why do you need this loop if you do below
for k in all_comments:
for z in all_undercom:
I have a feeling you don't really understand what the zip function does .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question