Instaloader,获取n个最新帖子



当前,下面的代码返回配置文件中的所有帖子:

profilename = 'oneplus'
loader = instaloader.Instaloader()    
profile = Profile.from_username(loader.context,profilename)
posts = profile.get_posts()

如果配置文件有3000条帖子,那么为instalander加载需要花费大量时间。

然而,我只想要最近的n个帖子,并且n<10,我该怎么做?

终于发现了一些方便的东西!

第一:

pip安装函数超时

想象一下你的功能是:

def sum(x,y): 
return x+y

然后像这样使用:

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)

对我来说,它就像一种魅力。

注意:如果你正在考虑下载帖子,请确保你还超时控制下载功能,因为该功能会扫描所有评论,这可能会很长。

如果遇到麻烦,请访问我找到它的网站:https://pypi.org/project/func-timeout/

干杯

最新更新