2017-02-22 34 views
0

我想獲得10條推文。我將計數設置爲5並應用「在範圍(2)中」。這意味着它應該檢索不超過10條推文。但是,這裏給了我15條推文,其中推文1至5的推文id是出現兩次。在Python中使用rest_api獲取推文

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條推文。是否有人可以幫助我在此使用rest_api在python enter image description here

回答

1

這是你print聲明

for pk in alltweet: 
         print pk.id 

loop第一次將打印5鳴叫。

下一次它prints5 + 5)tweets。

所以這個prints總計15推文。

也許你想給printloop搬出其他forloop一樣的:

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 
+0

@mithleshgupta你可以在我的另一個問題,請在這裏http://stackoverflow.com/questions/42399741/rest -API-使用鳴叫 - 都 - 而不是傾銷 - 到 - 文件中的Python – pk786