यहां एक न्यूनतम कामकाजी उदाहरण है (एक बार जब आप नकली कुंजी को वास्तविक लोगों के साथ प्रतिस्थापित करते हैं)।
import tweepy
from math import ceil
def get_authorization():
info = {"consumer_key": "A7055154EEFAKE31BD4E4F3B01F679",
"consumer_secret": "C8578274816FAEBEB3B5054447B6046F34B41F52",
"access_token": "15225728-3TtzidHIj6HCLBsaKX7fNpuEUGWHHmQJGeF",
"access_secret": "61E3D5BD2E1341FFD235DF58B9E2FC2C22BADAD0"}
auth = tweepy.OAuthHandler(info['consumer_key'], info['consumer_secret'])
auth.set_access_token(info['access_token'], info['access_secret'])
return auth
def get_tweets(query, n):
_max_queries = 100 # arbitrarily chosen value
api = tweepy.API(get_authorization())
tweets = tweet_batch = api.search(q=query, count=n)
ct = 1
while len(tweets) < n and ct < _max_queries:
print(len(tweets))
tweet_batch = api.search(q=query,
count=n - len(tweets),
max_id=tweet_batch.max_id)
tweets.extend(tweet_batch)
ct += 1
return tweets
नोट: मैं पाश के लिए एक का उपयोग कर प्रयास किया, लेकिन चहचहाना एपीआई कभी कभी 100 से कम परिणाम देता है (100 के लिए कहा जा रहा है के बावजूद, और 100 से उपलब्ध है)। मुझे यकीन नहीं है कि यह क्यों है, लेकिन यही कारण है कि मैंने tweet_batch खाली होने पर लूप को तोड़ने के लिए चेक शामिल नहीं किया था - आप query rate limit के रूप में स्वयं को ऐसा चेक जोड़ना चाहेंगे।
एक और ध्यान दें: आप की तरह wait_on_rate_limit=True
लागू द्वारा दर सीमा से टकराने से बचने कर सकते हैं ताकि
api = tweepy.API(get_authorization(), wait_on_rate_limit=True)
ऐसा लगता है कि एक गणना पैरामीटर है कि परिणामों की संख्या को नियंत्रित करता है की तरह है, लेकिन वहाँ एक रास्ता बस के सभी दिखाने के लिए है परिणाम? – user1893354