当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python Tweepy API.home_timeline()用法及代码示例


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

API.home_timeline()

的home_timeline()方法APITweepy模块中的类用于获取由身份验证用户和该用户的朋友发布的20个最新状态,包括转发。这等效于Web上的/timeline /home。

用法:API.home_timeline(parameters)

参数:

  • since_ids:仅获取比指定ID更新的状态。
  • max_ids:仅获取早于或等于指定ID的状态。
  • count:要获取的状态数,默认值为20。

返回值:状态类的对象列表



范例1:使用home_timeline()没有任何参数的方法。

# 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) 
  
# fetching the statuses 
statuses = api.home_timeline() 
  
# printing the screen names of each status 
for status in statuses:
    print(status.user.screen_name)

输出:

republic
AapGhumaKeLeLo_
vijaita
ABPNews
1911_1913_BakUp
BBCPolitics
SwarajyaMag
WIONews
YourAnonNews
thehill
AP
TimesNow
krystalball
People4Bernie
Holbornlolz
RealSaavedra
BenjaminPDixon
BJP4Haryana
BuzzFeed
MixedRaita

范例2:使用home_timeline()带有count参数的方法只能获取指定数量的状态。

# number of statuses to be fetched 
count = 5
  
# fetching the statuses 
statuses = api.home_timeline(count = count) 
  
# printing the screen names of each status 
for status in statuses:
    print(status.user.screen_name)

输出:

ANI
alexkotch
chrislongview
CNN
CGTNOfficial



相关用法


注:本文由纯净天空筛选整理自Yash_R大神的英文原创作品 Python – API.home_timeline() in Tweepy。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。