【问题标题】:Python Tweepy api: create_friendship() takes 1 positional argument but 2 were givenPython Tweepy api:create_friendship() 接受 1 个位置参数,但给出了 2 个
【发布时间】:2022-01-02 21:36:44
【问题描述】:

我想使用 tweepy 的 create_friendship 来关注特定帐户。 但我不断收到错误消息。

错误

  File "twitter_retweet_by_word.py", line 20, in <module>
    api.create_friendship("**********")
  File "/Users/*****/opt/anaconda3/lib/python3.8/site-packages/tweepy/api.py", line 46, in wrapper
    return method(*args, **kwargs)
TypeError: create_friendship() takes 1 positional argument but 2 were given

我在下面的链接中检查了类似的问题,但我不明白我必须做些什么才能使它工作。

Tweepy api: create_friendship() takes 1 positional argument but 2 were given

我的代码

import tweepy

consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""

# OAuth
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)




for status in api.search_tweets(q='Python RT', count=2):
    tweet_id = status.id
    print(tweet_id)
    user_id = status.user.screen_name
    print(user_id)
    api.create_friendship(user_id)

您能否告诉我我需要如何更改代码才能使其正常工作? 提前致谢。

python 版本:3.8.5 tweepy 版本:4.3.0

【问题讨论】:

    标签: python twitter


    【解决方案1】:

    据我了解,您必须这样调用该函数:

    api.create_friendship(user_id=user_id)
    

    create_friendship(self, kwargs**) 除了self 之外没有任何其他隐式参数,在调用函数时从不使用。

    create_friendship(user_id=some_variable) 告诉函数你要给出哪个参数,现在它知道它可以在自身内部使用的唯一参数是user_id

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-13
      • 2020-12-31
      • 2018-06-24
      • 2019-04-06
      • 2019-04-20
      • 2018-11-15
      • 2014-07-19
      相关资源
      最近更新 更多