From 750dda558d1edff33728f1d7ece7644aba06475f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=B3=E9=88=9E?= Date: Sat, 25 Jun 2022 18:55:47 +0800 Subject: [PATCH] Bug 30: Profile.from_username() login required. fix #30 --- src/scraper.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/scraper.py b/src/scraper.py index 15c813c..2ea6657 100644 --- a/src/scraper.py +++ b/src/scraper.py @@ -1,4 +1,5 @@ from instaloader.exceptions import LoginRequiredException +import sys import requests import time from types import NoneType @@ -17,12 +18,16 @@ def __init__(self, username: str, loader: Loader): def __get_profile(self) -> Profile: try: - # Use a new loader to get posts that do not require login. - self.profile = Profile.from_username(instaloader.Instaloader().context, self.username) + self.profile = Profile.from_username( + self.loader.context, self.username) return self.profile except requests.exceptions.ConnectionError: time.sleep(10) return self.__get_profile() + except instaloader.LoginRequiredException as e: + print(e, file=sys.stderr) + print(f'Instagram requires login when fetching the profile. This sometimes happens, not sure how often.', file=sys.stderr) + return self.profile def get_last_post(self) -> Post | NoneType: return next(self.get_posts(), None)