python - 不接受 Twitter Tweep API 中的关键字参数“用户”
问题描述
我遇到了 api.send.direct.message 代码的问题。
它抛出error; peError: send_direct_message()
了一个意外的关键字参数“用户”。
我尝试了多种选择,包括 screen_name = user.screen_name 但没有运气。
import tweepy
import time
# Authenticate to Twitter
auth = tweepy.OAuthHandler("Kr2pjbeG3hDC2",
"3nunhSaUZ9MeAHpmbEhuEEqSFmj2l2EfaMIzxu9")
auth.set_access_token("107550621-n7OrqgFx",
"mse3r98sEfziOO95qPCrnD0kUh9lJToxRJ3E0Uzmg")
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
for user in api.followers():
text="""Hi"""
try:
api.send_direct_message(user=user, text=text)
print(user.screen_name)
except tweepy.TweepError as e:
print(e.args[0][0]['code']) # prints 34
print(e.args[0][0]['message'])
continue
except StopIteration:
break
解决方案
根据文档,您应该指定收件人的 ID:
API.send_direct_message(recipient_id, text[, quick_reply_type][, attachment_type][, attachment_media_id])
您的代码应如下所示:
import tweepy
import time
# Authenticate to Twitter
auth = tweepy.OAuthHandler("Kr2pjbeG3hDC2",
"3nunhSaUZ9MeAHpmbEhuEEqSFmj2l2EfaMIzxu9")
auth.set_access_token("107550621-n7OrqgFx",
"mse3r98sEfziOO95qPCrnD0kUh9lJToxRJ3E0Uzmg")
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
for user in api.followers():
text="""Hi"""
try:
api.send_direct_message(recipient_id=user.id, text=text)
print(user.screen_name)
except tweepy.TweepError as e:
print(e.args[0][0]['code']) # prints 34
print(e.args[0][0]['message'])
continue
except StopIteration:
break
推荐阅读
- c# - C# 的实体框架迁移 API
- css - 三星 Galaxy S10 的媒体查询不工作
- wordpress - 如何获取多个数组的自定义元字段的值
- swift - “使用默认容器”未显示在 iCloud 功能中
- reactjs - ReactNative Navigation headerRight Toggle Drawer Error
- spring - 附加 packagesToScan 以及 persistence.xml 中的类
- javascript - 如何创建下一个和上一个按钮以使用数组存储作为对象的问题来选择测验中的问题?
- asp.net-core - 如果可为空的 int 为空,如何设置值
- python - 为什么我的两个参数的表达式值不正确?
- javascript - 制作宾果游戏。如何从一组中生成要显示的随机数,而不重复生成和显示哪个数字?