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

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

Python – Tweepy 中的 API.unsubscribe_list()

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

API.unsubscribe_list()

Tweepy 模块中 API 类的unsubscribe_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 unsubscribe_list() method
print("The number of subscribers before unsubscribe_list() method : " +
      str(api.get_list(list_id = list_id).subscriber_count))
 
# subscribing to the list
api.unsubscribe_list(list_id = list_id)
 
# number of subscribers after unsubscribe_list() method
print("The number of subscribers after unsubscribe_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 unsubscribe_list() method
print("The number of subscribers before unsubscribe_list() method : " +
      str(api.get_list(list_id = list_id).subscriber_count))
 
# unsubscribing to the list
api.unsubscribe_list(slug = slug, owner_screen_name = owner_screen_name)
 
# number of subscribers after unsubscribe_list() method
print("The number of subscribers after unsubscribe_list() method : " +
      str(api.get_list(list_id = list_id).subscriber_count))


输出 :

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

示例 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 unsubscribe_list() method
print("The number of subscribers before unsubscribe_list() method : " +
      str(api.get_list(list_id = list_id).subscriber_count))
 
# unsubscribing to the list
api.unsubscribe_list(slug = slug, owner_screen_name = owner_screen_name)
 
# number of subscribers after unsubscribe_list() method
print("The number of subscribers after unsubscribe_list() method : " +
      str(api.get_list(list_id = list_id).subscriber_count))

输出 :

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