You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tweepy调用api.get_user获取用户信息时出现TypeError参数数量错误的问题求助

解决Tweepy中api.get_user()的TypeError参数不匹配问题

你遇到的这个TypeError: get_user() takes 1 positional argument but 2 were given错误,核心原因是你使用的Tweepy版本要求调用get_user()时必须传入关键字参数,而不是直接传递位置参数。

在较新的Tweepy版本(包括V1.1的后期版本和V2版本)中,get_user()不再接受直接的位置参数,必须明确指定参数名:比如你传入的是用户名(字符串形式,像elonmusk),就要用screen_name作为参数名;如果是用户ID(数字),则用user_id。

另外你的代码里还有一个小错误:location = user_info.name.location是错误的,user_info.name是用户的昵称字符串,并没有location属性,应该直接取user_info.location。

修正后的完整代码

api = tweepy.API(auth)
userID_list = ["elonmusk", "BarackObama"]
for userxyz in userID_list:
    # 明确使用screen_name关键字参数传递用户名
    user_info = api.get_user(screen_name=userxyz)
    name = user_info.name
    description = user_info.description
    # 修正location的获取方式
    location = user_info.location
    followers_count = user_info.followers_count
    friends_count = user_info.friends_count
    
    # 可选:打印获取到的信息验证结果
    print(f"用户名:{userxyz}")
    print(f"昵称:{name}")
    print(f"个人简介:{description}")
    print(f"所在地:{location}")
    print(f"粉丝总数:{followers_count}")
    print(f"关注总数:{friends_count}\n")

额外说明

  • 如果你的userID_list中存储的是用户的数字ID(比如44196397是Elon Musk的用户ID),那么调用时要换成user_id参数:user_info = api.get_user(user_id=userxyz)
  • 不同Tweepy版本的API细节可能有差异,如果你需要更详细的参数说明,可以直接在Python中用help(tweepy.API.get_user)快速查看参数要求

内容的提问来源于stack exchange,提问作者user17456443

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.01 03:12:43