[英]Tweepy (Twitter API) Not Returning all Search Results
我正在使用Twitter的Tweepy搜索功能,出於某種原因搜索結果限制為15.這是我的代碼
results=api.search(q="Football",rpp=1000)
for result in results:
print "%s" %(clNormalizeString(result.text))
print len(results)
只返回15個結果。 它與不同的結果頁面有什么關系嗎?
問題更多的是關於Twitter API而不是tweepy本身。
根據文檔 , count
參數定義:
每頁返回的推文數量,最多為100個。默認為15.這以前是舊版Search API中的“rpp”參數。
僅供參考,您可以使用tweepy.Cursor
獲取分頁結果,如下所示:
import tweepy
auth = tweepy.OAuthHandler(..., ...)
auth.set_access_token(..., ...)
api = tweepy.API(auth)
for tweet in tweepy.Cursor(api.search,
q="google",
count=100,
result_type="recent",
include_entities=True,
lang="en").items():
print tweet.created_at, tweet.text
另見: https : //github.com/tweepy/tweepy/issues/197 。
希望有所幫助。
這是一個最小的工作示例(一旦你用真實的鑰匙替換假鑰匙)。
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
注意:我確實嘗試使用for循環,但是twitter api有時會返回少於100個結果(盡管被要求100,並且100可用)。 我不確定為什么會這樣,但這就是為什么我沒有包括檢查來打破循環,如果tweet_batch為空 - 你可能想要自己添加這樣的檢查,因為有一個查詢速率限制 。
另請注意:您可以通過調用wait_on_rate_limit=True
來避免達到速率限制
api = tweepy.API(get_authorization(), wait_on_rate_limit=True)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.