Python tweepy在两次约会之间收到推特
我有以下Python代码:Python tweepy在两次约会之间收到推特,python,tweepy,Python,Tweepy,我有以下Python代码: import tweepy 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) start_dat
import tweepy
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)
start_date = datetime.datetime(2018, 1, 19, 12, 00, 00)
end_date = datetime.datetime(2018, 1, 19, 13, 00, 00)
api = tweepy.API(auth)
for tweet in tweepy.Cursor(api.user_timeline, screen_name="@IBM", since=start_date, until=end_date).items():
print("ID TWEET: " + str(tweet.id))
有没有办法通过使用tweepy修改光标,在开始日期
和结束日期
之间获取推文
我已经尝试使用since=
和until=
参数,但它们都不起作用
首先,Twitter API不允许按时间搜索。简单地说,您可以做的是获取tweet并在Python中查看它们的时间戳,但这是非常低效的 您可以通过下面的代码段来实现这一点
consumerKey = "CONSUMER_KEY"
consumerSecret = "CONSUMER_SECRET"
accessToken = "ACCESS_TOKEN"
accessTokenSecret = "ACCESS_TOKEN_SECRET"
auth = tweepy.OAuthHandler(consumerKey, consumerSecret)
auth.set_access_token(accessToken, accessTokenSecret)
api = tweepy.API(auth)
username = sys.argv[1]
startDate = datetime.datetime(2011, 6, 1, 0, 0, 0)
endDate = datetime.datetime(2012, 1, 1, 0, 0, 0)
tweets = []
tmpTweets = api.user_timeline(username)
for tweet in tmpTweets:
if tweet.created_at < endDate and tweet.created_at > startDate:
tweets.append(tweet)
while (tmpTweets[-1].created_at > startDate):
tmpTweets = api.user_timeline(username, max_id = tmpTweets[-1].id)
for tweet in tmpTweets:
if tweet.created_at < endDate and tweet.created_at > startDate:
tweets.append(tweet)
consumerKey=“消费者密钥”
ConsumerCret=“消费者的秘密”
accessToken=“访问令牌”
accessTokenSecret=“访问令牌密钥”
auth=tweepy.OAuthHandler(consumerKey,ConsumerCret)
身份验证设置\u访问\u令牌(accessToken,accessTokenSecret)
api=tweepy.api(auth)
用户名=sys.argv[1]
startDate=datetime.datetime(2011,6,1,0,0,0)
endDate=datetime.datetime(2012,1,1,0,0,0)
tweets=[]
tmpTweets=api.user\u时间线(用户名)
对于tmpTweets中的tweet:
如果tweet.created_位于<结束日期,tweet.created_位于>开始日期:
tweets.append(tweet)
而(tmpTweets[-1]。在>开始日期创建):
tmpTweets=api.user\u时间线(用户名,max\u id=tmpTweets[-1].id)
对于tmpTweets中的tweet:
如果tweet.created_位于<结束日期,tweet.created_位于>开始日期:
tweets.append(tweet)
虽然效率很低。它可以工作,可以帮助我创建自己的机器人。我刚刚使用了until(可选操作符),它似乎工作得很好。我是这样用的:
tweets = tw.Cursor(api.search,
q=search_words,
lang="en",
since=date_since,
until=date_until,
result_type="recent"
).items(2)
直到
有时间限制:(…)搜索索引有7天的限制。换言之,超过一周的日期将找不到tweet。
这可能会有所帮助<代码>https://stackoverflow.com/questions/26205102/making-very-specific-time-requests-to-the-second-on-twitter-api-using-python或这一个https://gist.github.com/alexdeloy/fdb36ad251f70855d5d6
谢谢大家的回复!这对我也有用。请注意,我必须导入示例代码中未显示的datetime
。如何进行此操作以搜索标签并在开始日期和结束日期之间获取tweet。我认为您不能。您只能通过以下方式从给定的标签下载推文:search
twapi.search(q=query,count=100,因为\u id=since\u id,max\u id=str(last\u id-1),tweet_mode='extended')过去10天。唯一的解决办法是您知道tweet\u id
,无论Twitter API设置的时间限制如何,您都可以下载它。因为在3.8版中作为API.search的搜索参数被删除。但是直到仍然存在:返回在给定日期之前创建的tweet。。。。日期格式应为YYYY-MM D(包括刚开始日期)仅为当前系统日期提供数据。当我尝试也包括结束日期时,我得到一个错误,“赋值前引用的局部变量'csvFile'