【问题标题】:Python - Trouble with Twython apiPython - Twython api 的问题
【发布时间】:2014-10-06 13:09:45
【问题描述】:

我编写了一个使用 Twython 与 twitter 交互的 python 脚本。它将关注者列表下载到一个变量(关注者)中,并将我关注的人列表下载到另一个变量(朋友)中。然后它应该自动关注所有关注我但我尚未关注的人。

for fol in followers:
    if fol not in friends:
        twitter.create_friendship(fol)

我得到的错误是twitter.create_friendship 只需要一个参数,但它被赋予了两个。我看不出它是如何被赋予两个参数的,我只能看到一个。

【问题讨论】:

  • 这是create_friendship 的文档,它指向friendships.create 文档。
  • 当你在python中调用一个函数时,你调用它的对象作为第一个参数传递。这就是为什么所有类方法都将self作为第一个参数。

标签: python loops for-loop twython notin


【解决方案1】:

create_friendship() 是一个绑定方法,这意味着它只需要self 参数。它不接受任何其他位置参数,但你传入fol,现在给它两个参数(selffol)。

该方法应改为传递关键字参数:

twitter.create_friendship(user_id=fol)

如果 fol 是用户 ID,或者

twitter.create_friendship(screen_name=fol)

如果fol 是一个网名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-21
    • 2013-06-22
    • 2017-04-04
    • 1970-01-01
    • 2013-10-19
    • 2023-04-10
    • 1970-01-01
    • 2018-03-19
    相关资源
    最近更新 更多