Instaloader - 下载故事元数据时遇到问题

问题描述 投票:0回答:0

我正在尝试从用户名列表中下载故事元数据,但我的代码根本不起作用,我对 python 不太熟悉所以我不知道它有什么问题,我的主要目标是找到提到的故事某个用户名,如果我想下载那些故事或至少他的信息(发帖人的 ID 或用户名)。

我使用了那个代码: 导入安装器

创建 Instaloader 类的实例

loader = instaloader.Instaloader()

设置元数据文件的文件名模式

filename_pattern = '{date_utc:%Y-%m-%d}{owner}{shortcode}'

设置元数据文件的路径模式

post_metadata_path_pattern = '{owner}/{date_utc:%Y-%m-%d}{shortcode}.txt' storyitem_metadata_path_pattern = '{owner}/{date_utc:%Y-%m-%d}{shortcode}_storyitem.txt'

设置正则表达式模式来提取提及

mentions_pattern = r'{mentions}'

设置包含用户名列表的文件路径

usernames_file = 'usernames.txt'

从文件中读取用户名列表

with open(usernames_file, 'r') as f: usernames = [line.strip() for line in f.readlines()]

为每个用户下载每个故事的元数据

对于用户名中的用户名: # 设置元数据文件的输出目录 output_dir = f'{username}_metadata'

# Create the directory if it does not exist
if not os.path.exists(output_dir):
    os.makedirs(output_dir)

# Download the metadata for each story for the user
loader.download_stories(userids=[username],
                        filename_target=output_dir,
                        filename_pattern=filename_pattern,
                        post_metadata_txt_pattern=post_metadata_path_pattern,
                        storyitem_metadata_txt_pattern=storyitem_metadata_path_pattern,
                        fast_update=False,
                        download_video=False,
                        download_pictures=False,
                        storyitem_selector=instaloader.StoryItemSelector(hashtags=False),
                        download_stories=True,
                        max_count=None,
                        stories_callback=None,
                        postprocessor=None,
                        onerror=None,
                        ondownload=None,
                        **{'post': {'-M': None}, 'storyitem': {'-re': mentions_pattern}})
python instagram instaloader
© www.soinside.com 2019 - 2024. All rights reserved.