Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import instaloader
|
2 |
+
|
3 |
+
def fetch_instagram_posts(username: str, max_posts: int = 10):
|
4 |
+
"""
|
5 |
+
Fetch Instagram posts from a public profile.
|
6 |
+
|
7 |
+
Args:
|
8 |
+
username (str): The username of the Instagram profile.
|
9 |
+
max_posts (int): The maximum number of posts to fetch.
|
10 |
+
|
11 |
+
Returns:
|
12 |
+
None
|
13 |
+
"""
|
14 |
+
# Initialize Instaloader
|
15 |
+
L = instaloader.Instaloader()
|
16 |
+
|
17 |
+
try:
|
18 |
+
# Load the profile
|
19 |
+
print(f"Fetching posts for user: {username}")
|
20 |
+
profile = instaloader.Profile.from_username(L.context, username)
|
21 |
+
|
22 |
+
# Counter to limit the number of posts
|
23 |
+
count = 0
|
24 |
+
|
25 |
+
# Loop through the posts
|
26 |
+
for post in profile.get_posts():
|
27 |
+
print(f"\nPost {count + 1}:")
|
28 |
+
print(f"Caption: {post.caption or 'No Caption'}")
|
29 |
+
print(f"Likes: {post.likes}")
|
30 |
+
print(f"Comments: {post.comments}")
|
31 |
+
print(f"Posted on: {post.date_utc}")
|
32 |
+
print(f"Image URL: {post.url}")
|
33 |
+
|
34 |
+
# Save the post image locally
|
35 |
+
L.download_post(post, target=profile.username)
|
36 |
+
|
37 |
+
# Increment the counter
|
38 |
+
count += 1
|
39 |
+
if count >= max_posts:
|
40 |
+
break
|
41 |
+
|
42 |
+
print(f"\nFetched {count} posts for user: {username}.")
|
43 |
+
|
44 |
+
except instaloader.exceptions.ProfileNotExistsException:
|
45 |
+
print(f"Error: Profile '{username}' does not exist.")
|
46 |
+
except instaloader.exceptions.ConnectionException:
|
47 |
+
print("Error: Unable to connect to Instagram. Check your internet connection.")
|
48 |
+
except Exception as e:
|
49 |
+
print(f"Unexpected error: {e}")
|
50 |
+
|
51 |
+
|
52 |
+
if __name__ == "__main__":
|
53 |
+
# Hardcode the username and number of posts to fetch
|
54 |
+
username = "bionsdesignco" # Replace with the desired Instagram username
|
55 |
+
max_posts = 5 # Replace with the desired number of posts to fetch
|
56 |
+
|
57 |
+
# Fetch posts
|
58 |
+
fetch_instagram_posts(username, max_posts)
|