'Instaloader, get n most recent posts
Currently, the below code returns all the posts from the profile:
profilename = 'oneplus'
loader = instaloader.Instaloader()
profile = Profile.from_username(loader.context,profilename)
posts = profile.get_posts()
If the profile has, say 3000 posts, it takes a lot of time to load for instaloader.
However, I just want most recent n posts, and n<10, how do I do that?
Solution 1:[1]
finally came across something handy!
Firstly:
pip install func_timeout
Imagine your function was:
def sum(x,y):
return x+y
Then use it like so:
from func_timeout import func_timeout, FunctionTimedOut
try:
seconds = 5 # times out after 5 secs
result=func_timeout(seconds,sum,args=(5,3))
except FunctionTimedOut:
print('timeout')
except Exception as e:
print(e)
For me, it works like a charm.
Note: if you are thinking of downloading the posts, make sure you also timeout control the download function, as the function scans all the comments, which can get pretty lengthy.
If in trouble, visit the website where I found it: https://pypi.org/project/func-timeout/
Cheers
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tomas1906 |
