>本教程演示瞭如何利用Python的Twitter庫與Twitter API進行交互,從而直接從Python環境中啟用數據提取並郵政控制。 我們將介紹連接到API,下載推文和用戶列表,同時發布多個推文並執行自定義搜索。
1。連接到Twitter api
本教程使用ipython。 要連接,您將需要您的消費者密鑰,消費者秘密,訪問令牌密鑰以及訪問令牌秘密。 這些是通過在apps.twitter.com上創建twitter應用程序來獲得的。
安裝庫:
然後,導入庫並輸入您的憑據:python-twitter
pip install python-twitter
import twitter api = twitter.Api(consumer_key='your_consumer_key', consumer_secret='your_consumer_secret', access_token_key='your_access_token_key', access_token_secret='your_access_token_secret') print(api.VerifyCredentials())
使用
檢索推文:
api.GetUserTimeline()
statuses = api.GetUserTimeline(screen_name='Michael Grogan') print([s.text for s in statuses])
>
和:>
api.GetFriends()
api.GetFollowers()
限制使用total_count
檢索的用戶數:
pip install python-twitter
4。發布多個推文
使用api.PostUpdate()
>發布多個推文:
import twitter api = twitter.Api(consumer_key='your_consumer_key', consumer_secret='your_consumer_secret', access_token_key='your_access_token_key', access_token_secret='your_access_token_secret') print(api.VerifyCredentials())
5。搜索推文
> api.GetSearch()
函數允許進行強大的搜索。 示例:自2016年11月21日以來搜索“ BigData”,將結果限制為10:
statuses = api.GetUserTimeline(screen_name='Michael Grogan') print([s.text for s in statuses])
users = api.GetFriends() print([u.name for u in users]) followers = api.GetFollowers() print([f.name for f in followers])
數據使用和結論:
常見問題(FAQS)
> (原始輸入的FAQS部分保留在此處)
我如何設置一個Twitter開發人員帳戶?>
>要設置一個Twitter開發人員帳戶,您需要轉到Twitter開發人員的網站併申請開發人員帳戶。您將需要填寫申請表,詳細說明您計劃如何使用Twitter數據。批准您的應用程序後,您可以創建一個新項目並生成API密鑰。
>要使用Twitter API發布一條推文,您需要向“狀態/更新”端點提出發布請求。推文文本應包含在請求的“狀態”參數中。
>>
如何處理Twitter API中的速率限制?如何使用Twitter API搜索推文?
>您可以使用Twitter API的“搜索/推文”端點搜索推文。您可以在請求的“ Q”參數中指定搜索條件。>實時流推文,您可以使用Twitter流動API的“狀態/過濾器”端點。您可以指定諸如“軌道”之類的過濾條件,以通過關鍵字過濾Tweet。
>>
我如何將Twitter API與Python一起使用?以上是如何通過Python創建Twitter應用程序和API接口的詳細內容。更多資訊請關注PHP中文網其他相關文章!