📜  Python Tweepy – 获取推文的来源

📅  最后修改于: 2022-05-13 01:54:41.674000             🧑  作者: Mango

Python Tweepy – 获取推文的来源

在本文中,我们将了解如何获取状态/推文的来源。推文的来源告诉我们推文是如何发布的。来源的一些例子是:

  • 安卓版推特
  • iPhone 版推特
  • 推特网络应用

Status 对象的 source 属性为我们提供了状态的来源。

在 GUI 中识别状态的来源:

在上述状态中,状态的来源是:Twitter for Android

示例 1:考虑以下状态:

我们将使用状态 ID 来获取状态。上述状态的状态 ID 为 1272771459249844224。

# import the module
import tweepy
  
# assign the values accordingly
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
  
# authorization of consumer key and consumer secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  
# set access to user's access key and access secret 
auth.set_access_token(access_token, access_token_secret)
  
# calling the api 
api = tweepy.API(auth)
  
# the ID of the status
id = 1272771459249844224
  
# fetching the status
status = api.get_status(id)
  
# fetching the source attribute
source = status.source 
  
print("The source of the status is : " + source)

输出 :

The source of the status is : Twitter for Android

示例 2:考虑以下状态:

我们将使用状态 ID 来获取状态。上述状态的状态 ID 为 1273112322773581824。

# the ID of the status
id = 1273112322773581824
  
# fetching the status
status = api.get_status(id
  
# fetching the source attribute
source = status.source 
  
print("The source of the status is : " + source)

输出 :

The source of the status is : Twitter Web App