A
A
Alexander Kolotov2021-04-17 11:47:43
Python
Alexander Kolotov, 2021-04-17 11:47:43

How to store a list of codes (links, numbers, etc.) for reading, adding a new element, searching by element in python?

Hello, please tell me how can I be?

By making a script that every hour will check for new posts in the profile, and if there is one, then through the vk api it was published in the community.

I work with insta through InstaLoader and get all the posts through the profile.get_posts() command. I receive each post in the CNrZpWTBisu format,

and then a problem arose: I cannot check the result of profile.get_posts() with my list parsed in advance.

How can I make the lists check?

import instaloader
from instaloader import Profile, Post

profile_get_posts = ['CNrZpWTBisu', 'CNm_DxEheWO', 'CNc4iMUBq0T', 'CNXD011hE6t', 'CNH8ASSBT9A', 'CM_0D-zB_iw', 'CM1dTUSB-9X', 'CMrzkvFBWSq', 'CMU0QAQqjVC', 'CMKJ0r-BjZC', 'CMB02IPh9cV', 'CL3jX1mh7uB', 'CLoMIT4hdND']

# метод проверки наличия новых постов
def scrapImageAddresses(user_id):
    profile = Profile.from_username(loader.context, "user_id")
    with open('posted_posts.txt', 'r') as file:
        for post in profile_get_posts:
            post = str(post)
            print(111111, post)
            if post not in file:
                print(f'{post} этого поста нет')
                # file_text.insert(0, f'{post_shortcode}\n')
                # with open('posted_posts.txt', 'w') as file:
                #     file.writelines(file_text)
                # Тут этот цикл заканчивается и делает return
                return post
            elif post in file: print('Это старый пост')

        return False #Если новых постов вообще нет


I use a txt file to store and write post code strings.

CNrZpWTBisu
CNm_DxEheWO
CNc4iMUBq0T
CNXD011hE6t
CNH8ASSBT9A
CM_0D-zB_iw
CM1dTUSB-9X
CMrzkvFBWSq
CMU0QAQqjVC
CMKJ0r-BjZC
CMB02IPh9cV

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-04-17
@kolotovalexander

The file must be read first.

with open('posted_posts.txt', 'r') as file:
    old_posts = file.read()

for post in profile_get_posts:
    if post not in old_posts:
        print(f'{post} этого поста нет')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question