首页 > 解决方案 > 不接受 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  

标签: pythontweepy

解决方案


根据文档,您应该指定收件人的 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  

推荐阅读