1

我正在尝试使用 Twython 在 python 中使用 Twitter API,并且我正在观察一些对我来说似乎很奇怪的行为。

如果我运行以下代码...

from twython import Twython
from random import randint

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) # In the actual code, I obviously assign these, but I can't disclose them here, so the code won't work...

user_id = randint(1,250000000)
twitter_user = twitter.lookup_user(user_id)

我得到这个错误。

Traceback (most recent call last):

File "Twitter_API_Extraction.py", line 76, in <module>
twitter_user = twitter.lookup_user(user_id) # returns a list of dictionaries with all the users requested
TypeError: lookup_user() takes exactly 1 argument (2 given)

Twython 文档表明我只需要传递用户 ID 或屏幕名称 ( https://twython.readthedocs.org/en/latest/api.html )。一些谷歌搜索表明这个错误通常意味着我需要将self作为第一个参数传递,但我不太明白为什么。

但是,如果我改用以下作业...

twitter_user = twitter.lookup_user(user_id = randint(1,250000000))

一切都是玫瑰。我不知道为什么会这样,当我尝试使用相同的 lookup_user 函数访问关注者时,这是代码后面的一个问题。

任何有关触发此错误的原因以及我如何通过函数调用中的赋值绕过它的任何说明都将不胜感激!

4

2 回答 2

3

根据API 文档

lookup_user(**params)

根据传递给 user_id 和/或 screen_name 参数的逗号分隔值指定,为每个请求返回最多 100 个用户的完全水合用户对象。

**语法(文档意味着您需要提供命名参数(即f(a=b)),在这种情况下user_id和/或screen_name

在您的第一次尝试中,您尝试传递一个位置参数(即f(a)),该函数未设置为该参数。

于 2014-02-28T22:14:49.553 回答
2

API 声明lookup_user只接受关键字参数。关键字参数采用keyword=value您正在使用的形式lookup_user(user_id=randint(1,...))。这意味着您不能传递位置参数,这就是您正在使用的lookup_user(userid).

于 2014-02-28T22:14:52.690 回答