Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用tweepy获取用户的最新消息_Python_Twitter_Tweepy - Fatal编程技术网

Python 使用tweepy获取用户的最新消息

Python 使用tweepy获取用户的最新消息,python,twitter,tweepy,Python,Twitter,Tweepy,所以我有这个代码-> import tweepy ckey = '' csecret = '' atoken = '' asecret = '' auth = tweepy.OAuthHandler(ckey, csecret) auth.set_access_token(atoken, asecret) api = tweepy.API(auth) recent_post = api.user_timeline(screen_name = 'DropSentry', count =

所以我有这个代码->

import tweepy

ckey = ''
csecret = ''
atoken = ''
asecret = ''



auth = tweepy.OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)

api = tweepy.API(auth)

recent_post = api.user_timeline(screen_name = 'DropSentry', count = 1, include_rts = True)

print(recent_post)

打印用户最近的帖子。但是,有没有办法全天候运行此代码?例如,每当用户发布新内容时,我希望再次打印我的代码。

方法user\u timeline中的参数'since\u id'可以帮助您执行此操作

您需要获取用户发布的最后一个状态的id,并在参数'since_id'中给出它

recent_id = 1388810249122062337 #hardcode the last recent post id from the user
while True:
  recent_post = api.user_timeline(screen_name = 'DropSentry', count = 1, since_id=recent_id, include_rts = True)
  if recent_post:
    print(recent_post)
    recent_id = recent_post[0].id
  time.sleep(10) # To avoid spamming the API, you can put the number of seconds you want
但如果用户在10秒内发布多条消息,这段代码将丢失消息。因此,您还可以同时从用户处获取所有消息并将其全部打印出来

recent_id = 1388810249122062337 #hardcode the last recent post id from the user
while True:
  recent_posts = api.user_timeline(screen_name = 'DropSentry', since_id=recent_id, include_rts = True)
  if recent_posts:
    for recent_post in recent_posts:
      print(recent_post)
      recent_id = recent_post.id
  time.sleep(10) # To avoid spamming the API, you can put the number of seconds you want

这有点奇怪,我一直试图使用该代码并得到这个错误:SyntaxError:十进制整数文本中的前导零是不允许的;为八进制整数使用0o前缀它的奇怪I测试,最近的_id=138810249122062337,它对第一个代码段有效,它不是最近的_post.id,而是最近的_post[0].id。我编辑答案我只有一个问题如果我执行了代码,它会给我所有的信息,但我只需要“文本”怎么做?在第一个代码段中,在第二次打印(recent_post[0].text)中打印(recent_post[0].text),这是否回答了您的问题?