2017-02-02 11 views
0

これは私を狂ってしまいます。以下に示すように、私はを試して、単純なwhileループを使用してtweepy検索を実行し、それらをデータフレームに追加しようとしています。しかし何らかの理由で、100個のつぶやきの最初のセットを引っ張った後、新しい検索を実行するのではなく、そのセットを繰り返すだけです。アドバイスをいただければ幸いです。Tweepy Search With While Loop

import sys 
import csv 
import pandas as pd 
import tweepy 
from tweepy import OAuthHandler 

consumer_key = '' 
consumer_secret = '' 
access_token = '' 
access_secret = '' 

auth = OAuthHandler(consumer_key, consumer_secret) 
auth.set_access_token(access_token, access_secret) 

api = tweepy.API(auth) 

num_results = 200 
result_count = 0 
last_id = None 
df = pd.DataFrame(columns=['Name', 'Location', 'Followers', 'Text', 'Coorinates']) 

while result_count < num_results: 
    result = api.search(q='',count=100, geocode= "38.996918,-104.995826,190mi", since_id = last_id) 
    for tweet in result: 
     user = tweet.user 
     last_id = tweet.id_str 
     name = user.name 
     friends = user.friends_count 
     followers = user.followers_count 
     text = tweet.text.encode('utf-8') 
     location = user.location 
     coordinates = tweet.coordinates 
     df.loc[result_count] = pd.Series({'Name':name, 'Location':location, 'Followers':followers, 'Text':text, 'Coordinates':coordinates}) 
     print(text) 
     result_count += 1 

# Save to Excel 
print("Writing all tables to Excel...") 
df.to_csv('out.csv') 
print("Excel Export Complete.") 

答えて

0

API.searchメソッドは、指定されたクエリに一致するツイートを返します。ストリーミングAPiではないので、すべてのデータを一度に返します。
さらに、クエリパラメータには、取得するステータスの数を指定するcountが追加されています。

したがって、問合せでは、繰り返しごとに完全なセットの最初の100データを戻してしまうという問題があります。

私はあなたが私を知ってみましょう。この

result = api.search(q='', geocode= "38.996918,-104.995826,190mi", since_id = last_id) 
for tweet in result: 
    user = tweet.user 
    last_id = tweet.id_str 
    name = user.name 
    friends = user.friends_count 
    followers = user.followers_count 
    text = tweet.text.encode('utf-8') 
    location = user.location 
    coordinates = tweet.coordinates 
    df.loc[result_count] = pd.Series({'Name':name, 'Location':location, 'Followers':followers, 'Text':text, 'Coordinates':coordinates}) 
    print(text) 

のようなもので、コードを変更することをお勧めします。

+0

私は検索クエリから 'count'パラメータを除外しようとしましたが、それは15の結果しか返しません。 tweepyドキュメントからの私の理解は、 'count'パラメータを含めることによって100までの値をとり、より多くの結果をコンパイルする15の要求を行うことができるということです。私はその両方の制限がTwitter APIによって強制されると信じています、はい? 私は、 'since_id'パラメータが、同じトップ100の結果を何度も何度も何度も引っ張って来ないようにするためにそこに存在するとも考えましたか? 私は結果を引き出すためにストリームに切り替えることを検討していますが、私の目標はつぶやきの「ランダム」な選択を集めることです。 – bengen343

+0

遅い答えを申し訳ありません、btw、countパラメータで約15のリクエストをどこで読んだのですか? since_idパラメータを削除しようとすると、IDが指定されたIDよりも大きい(つまり、より新しい)ステータスしか返されないことがあります。 – Giordano