📌  相关文章
📜  Python – Tweepy 中的 API.subscribe_list()

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

Python – Tweepy 中的 API.subscribe_list()

Twitter是一个流行的社交网络,用户在其中分享称为推文的消息。 Twitter 允许我们使用 Twitter API 或Tweepy挖掘任何用户的数据。数据将是从用户那里提取的推文。首先要做的是从 twitter 开发人员那里轻松获得每个用户可用的消费者密钥、消费者密钥、访问密钥和访问密钥。这些密钥将帮助 API 进行身份验证。

API.subscribe_list()

Tweepy 模块中 API 类的subscribe_list()方法用于作为认证用户订阅指定列表。

示例 1:订阅列表。

Python3
# 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 list
list_id =
 
# number of subscribers before subscribe_list() method
print("The number of subscribers before subscribe_list() method : " +
      str(api.get_list(list_id = list_id).subscriber_count))
 
# subscribing to the list
api.subscribe_list(list_id = list_id)
 
# number of subscribers after subscribe_list() method
print("The number of subscribers after subscribe_list() method : " +
      str(api.get_list(list_id = list_id).subscriber_count))


Python3
# the ID of the list
list_id = 4343
 
# the slug of the list
slug = "thought-leaders"
 
# the screen name of the owner of the list
owner_screen_name = "kitson"
 
# number of subscribers before subscribe_list() method
print("The number of subscribers before subscribe_list() method : " +
      str(api.get_list(list_id = list_id).subscriber_count))
 
# subscribing to the list
api.subscribe_list(slug = slug, owner_screen_name = owner_screen_name)
 
# number of subscribers after subscribe_list() method
print("The number of subscribers after subscribe_list() method : " +
      str(api.get_list(list_id = list_id).subscriber_count))


输出 :

The number of subscribers before subscribe_list() method : 0
The number of subscribers after subscribe_list() method : 1

示例 2:通过其 slug 名称订阅其他人的列表。

Python3

# the ID of the list
list_id = 4343
 
# the slug of the list
slug = "thought-leaders"
 
# the screen name of the owner of the list
owner_screen_name = "kitson"
 
# number of subscribers before subscribe_list() method
print("The number of subscribers before subscribe_list() method : " +
      str(api.get_list(list_id = list_id).subscriber_count))
 
# subscribing to the list
api.subscribe_list(slug = slug, owner_screen_name = owner_screen_name)
 
# number of subscribers after subscribe_list() method
print("The number of subscribers after subscribe_list() method : " +
      str(api.get_list(list_id = list_id).subscriber_count))

输出 :

The number of subscribers before subscribe_list() method : 4064
The number of subscribers after subscribe_list() method : 4065