Skip to main content
 首页 » 编程设计

python之Twython:如何在以下代码中喜欢照片和关注个人资料

2025年04月02日15bjzhanghao

您将如何添加点赞转推的帖子以及关注发帖用户的功能。

naughty_words = [" -RT"] 
good_words = ["CSGO", "skins", "csgo giveaway" "csgogiveaway",      "CSGOGiveaway", "Giveaway"] 
filter = " OR ".join(good_words) 
blacklist = " -".join(naughty_words) 
keywords = filter + blacklist 
 
twitter = Twython(app_key, app_secret, oauth_token, oauth_token_secret) 
 
search_results = twitter.search(q=keywords, count=20) 
try: 
    for tweet in search_results["statuses"]: 
        try: 
            twitter.retweet(id = tweet["id_str"]) 
 
        except TwythonError as e: 
            "print e" 
except TwythonError as e: 
    "print e" 

请您参考如下方法:

要在知道推文 ID 后喜欢推文,请使用:

twitter.create_favorite(id=tweet_id) 

要关注 发布推文的用户:

tweet = twitter.show_status(id=tweet_id) 
twitter.create_friendship(user_id=tweet['user']['id']) 

编辑: 如果愿意,您可以在一行中关注用户,方法是将“tweet”替换为我们用来检索推文的代码。我已经测试过了,它确实有效:

twitter.create_friendship(user_id=twitter.show_status(id=tweet_id)['user']['id'])