在python中使用rest_api获取推文

2024-09-27 00:19:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力获得10条推特。我将计数设置为5,并应用“在范围(2)内”。这意味着它应该检索不超过10条tweet。但是,这里它给了我15条tweets,其中tweets id的tweets 1到5出现了两次

alltweet=[]
def rest_query_ex3():
            query = "road"
                    geo = "42.6525,-73.7572,9mi"
                    MAX_ID = None
                    for it in range(2):  # should Retrieve up to 10 tweets
                      tweets = myApi.search(q=query, geocode=geo, count=5, max_id=MAX_ID)
                      if tweets:
                        MAX_ID= tweets[-1].id
                        alltweet.extend(tweets)
                        for pk in alltweet:
                          print pk.id


    if __name__ == '__main__':

        rest_query_ex3()

在这张图片中,一些推特id正在重复,给了我超过10条推特。有人能在python中使用rest_api帮助我吗 enter image description here


Tags: inrestidforifquerymaxtweets
1条回答
网友
1楼 · 发布于 2024-09-27 00:19:25

这是您的print语句

for pk in alltweet:
                      print pk.id

第一次在loop中,它将打印5条推文

下次它prints5 + 5)推特时

所以这个prints总计15推特

也许您希望将print{}从另一个for{}中移出,如下所示:

 for it in range(2):  # should Retrieve up to 10 tweets
                      tweets = myApi.search(q=query, geocode=geo, count=5, max_id=MAX_ID)
                      if tweets:
                        MAX_ID= tweets[-1].id
                        alltweet.extend(tweets)
 for pk in alltweet:
     print pk.id

相关问题 更多 >

    热门问题