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