📅  最后修改于: 2023-12-03 14:50:44.304000             🧑  作者: Mango
唐纳德特朗普是美国前总统,也是一位极具争议性的政治人物。Twitter是他最为活跃的发声平台之一,曾多次因为推特发言引起争议。
Twitter提供了REST API和Streaming API两种服务,可用于获取推特信息。使用REST API可以获取用户信息、推特信息、趋势等数据,而使用Streaming API可以实时订阅推特流。同时,Twitter还提供了开发者文档,详细介绍了API的使用方法和限制。
下面是使用Python的Tweepy库获取唐纳德特朗普最新的10条推特信息的示例代码:
import tweepy
# 各种认证信息
consumer_key = "<your consumer key>"
consumer_secret = "<your consumer secret>"
access_token = "<your access token>"
access_token_secret = "<your access token secret>"
# 进行OAuth认证
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
# 构建API对象
api = tweepy.API(auth)
# 获取唐纳德特朗普最新的10条推特信息
tweets = api.user_timeline("realDonaldTrump", count=10)
# 打印推特信息
for tweet in tweets:
print(tweet.text)
print("-" * 50)
输出结果:
I will be delivering a speech on the future of the Republican Party and the conservative movement tonight at #CPAC20… https://t.co/a40mEWJuwu
--------------------------------------------------
THE BEST IS YET TO COME!
--------------------------------------------------
https://t.co/JDS4zUXXJG
--------------------------------------------------
pic.twitter.com/11nzKwOCtU
--------------------------------------------------
RT @WhiteHouse: The United States and India have a great economic partnership. President @realDonaldTrump looks forward to growing even st…
--------------------------------------------------
RT @WhiteHouse: In Afghanistan, fewer American troops are serving in harm's way than ever before—thanks to President @realDonaldTrump’s ne…
--------------------------------------------------
RT @Mike_Pence: President @realDonaldTrump has made clear that our Administration will always stand with the men and women who wear the uni…
--------------------------------------------------
RT @Mike_Pence: Momentous day for America’s space program. Today, our National Space Council is unveiling the final report on how America c…
--------------------------------------------------
Congratulations to Attorney General Bill Barr for taking charge of a case that was totally out of control and perhaps… https://t.co/rpFQco19KE
--------------------------------------------------
95% Approval Rating in the Republican Party. Thank you! Just won two Congressional Seats in North Carolina, & a Governors runoff in Louisiana
--------------------------------------------------
使用Twitter API有一些限制,需要开发者注意,比如请求速率、请求频率、请求配额等。开发者需要根据自己的需求选择合适的API服务和数据获取方式,同时遵守Twitter的开发者协议和使用条件。