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

运行Tweepy代码出现AttributeError: 'str'对象无'request'属性如何解决

报错原因

代码的API调用方式不符合Tweepy的使用规则,具体问题有两处:

  • 你已经通过tweepy.API(auth)创建了完成鉴权的API实例对象api,但调用get_friend_ids方法时错误地直接调用了tweepy.API类本身的方法,而非实例的方法。类直接调用实例方法时,第一个参数会被识别为self(要求是API实例对象),你传入的screen_name是字符串类型,方法内部执行self.request时,字符串没有对应的request属性,就触发了本次报错。
  • 后续循环里的tweepy.api.get_user也是错误用法,同样需要用你已经实例化的api对象调用。
修复方案

只需要修改两处错误的API调用语句即可,修改后的完整代码如下:

# import the module
import tweepy

# assign the values accordingly
consumer_key = '...'
consumer_secret = '...'
access_token = '...'
access_token_secret = '...'

# authorization of consumer key and consumer secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)

# set access to user's access key and access secret
auth.set_access_token(access_token, access_token_secret)

# calling the api
api = tweepy.API(auth)

# screen name of the user
screen_name = "coinbase"

# 改为用实例api调用方法
friends = api.get_friend_ids(screen_name=screen_name)

print(screen_name + " is following :")
for friend in friends:
    # 改为用实例api调用方法
    print(api.get_user(user_id=friend).screen_name)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 00:24:01