【问题标题】:fetching tweets using rest_api in python在 python 中使用 rest_api 获取推文
【发布时间】:2017-07-12 03:32:10
【问题描述】:

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

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

【问题讨论】:

    标签: python json python-2.7 python-3.x dataset


    【解决方案1】:

    这是您的print 声明

    for pk in alltweet:
                          print pk.id
    

    第一次在loop 中将打印5 推文。

    下次再发prints (5 + 5) 推文。

    所以这prints 总共15 推文。

    也许您想将print loop 移出另一个for loop 喜欢:

     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
    

    【讨论】:

    猜你喜欢
    • 2017-07-12
    • 2017-09-30
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    • 2021-12-13
    • 2015-06-18
    • 2019-09-08
    • 2017-10-21
    相关资源
    最近更新 更多