我正在尝试设置从Twitter收到的最大推文数量。
我尝试过count=100
,但似乎对我没有用
import tweepy
api_key = ''
api_secret = ''
access_token = ''
access_token_secret = ''
auth = tweepy.OAuthHandler(api_key, api_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
public_tweets = api.home_timeline()
for tweet in public_tweets:
print tweet.text
for tweet in tweepy.Cursor(api.search, q="google", rpp=100, count=20, result_type="recent", include_entities=True, lang="en").items():
print tweet.created_at, tweet.text
使用items
设置您要施加的限制。
for tweet in tweepy.Cursor(api.search, q="google", rpp=100, count=20, result_type="recent", include_entities=True, lang="en").items(200)
将推文的数量限制为200。
[在寻找答案以获取最大数量的搜索查询推文时,我点击了此页面。我正在共享tweepy版本3.8的代码段。
import tweepy
import json
from datetime import datetime as dt
query = 'stackoverflow'
#today's date; used only for file name
date_untill = '2020-04-28'
file_name = query.lower().replace(' ', '_') + '_untill_' + date_untill +'.txt'
# -----your keys here--------
consumer_key =
consumer_secret =
access_token =
access_token_secret =
#----------------------------
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True, proxy="https://if-behind-proxy-:port")
# Open/Create a file to append data
csv_file = open(file_name, 'a', encoding='utf-8')
print('starting download. At time', str(dt.now()))
print('output file is: ', file_name)
#starting from the very beginning whatever that may be
since_id_val = -1
while True:
for tweet in tweepy.Cursor(api.search, q=query, since_id=since_id_val, count=100).items(): # removed ,lang='en'
# print (tweet.created_at, tweet.text)
json_str = tweet._json
json.dump(json_str, csv_file)
csv_file.write('\n')
if not tweet:
break
# last returned tweet is the most recent seen so far
print('last_tweet_id is: ', str(tweet.id))
since_id_val = tweet.id
print('Info...last downloaded tweet id ', tweet.id_str, ';created_at ', tweet.created_at)
print('##Completed download, exiting at ', dt.now())
希望这对某人有帮助。