twitterで自分の固定ツイートをリツイートした人に自動でDmを送るpythonのプログラムを作ったのですが
python
import json import time import random from requests_oauthlib import OAuth1Session import tweepy api_key = ‘——‘ api_secret = ‘—‘ access_key = ‘—‘ access_secret = ‘—‘ twitter = OAuth1Session(api_key, api_secret, access_key, access_secret) auth = tweepy.OAuthHandler(api_key, api_secret) auth.set_access_token(access_key, access_secret) api = tweepy.API(auth) random_min_time = 300 # sleepでとり得る最小値 random_max_time = 400 # sleepでとり得る最大値 endpoint = 'https://api.twitter.com/2/users?ids=----- params = {'expansions': 'pinned_tweet_id'} res = twitter.get(endpoint, params=params) dic_me = json.loads(res.text) print(dic_me['data'][0]['pinned_tweet_id']) print(dic_me['includes']['tweets'][0]['text']) tweet_id = dic_me['data'][0]['pinned_tweet_id'] endpoint = 'https://api.twitter.com/1.1/statuses/retweeters/ids.json?id=' + tweet_id + '&stringify_ids=true' res = twitter.get(endpoint) dic_users = json.loads(res.text) for num in range(len(dic_users['ids'])): account_name = dic_users['ids'][num] user_id = api.user_timeline(account_name, count=1)[0].user.id try: api.send_direct_message(recipient_id=user_id, text=‘hello) print('メッセージを送信しました:', account_name) except Exception as e: print("Error!!!", e) except StopIteration: continue sleep_random_time = random.randint(random_min_time, random_max_time) print(account_name, ': ', ' 次のDM送信まで', sleep_random_time, '秒待ちます') time.sleep(sleep_random_time)**
Traceback (most recent call last):
File "automackforalpha.py", line 55, in <module>
user_id = api.user_timeline(account_name, count=1)[0].user.id
File "/usr/local/lib/python3.8/dist-packages/tweepy/api.py", line 33, in wrapper
return method(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/tweepy/api.py", line 46, in wrapper
return method(*args, **kwargs)
TypeError: user_timeline() takes 1 positional argument but 2 were given
というエラーが出て直りません
引数が違うらしいのでselfをつけてみても直りません
user_timeline の API ドキュメントを眺めてみると、
tweepy 4.1.0 documentation
https://docs.tweepy.org/en/v4.1.0/api.html#tweepy.API.user_timeline
positional parameter を受け付けない様です("1 positional argument" は self を指しています)。
なので、
user_id = api.user_timeline(screen_name=account_name, count=1)[0].user.id
を試してみて下さい。
このままではコードが読めないので、質問を編集し、</>(コードの挿入)ボタンを押し、出てくる’’’の枠の中にコードを貼り付けてください
@melianさんの回答の様に
変えてみたら
/usr/local/lib/python3.7/dist-packages/tweepy/binder.py in _call(*args, **kwargs)
251 return method
252 else:
--> 253 return method.execute()
254 finally:
255 method.session.close()
/usr/local/lib/python3.7/dist-packages/tweepy/binder.py in execute(self)
232 raise RateLimitError(error_msg, resp)
233 else:
--> 234 raise TweepError(error_msg, resp, api_code=api_error_code)
235
236 # Parse the response payload
TweepError: [{'code': 34, 'message': 'Sorry, that page does not exist.'}]
というのが出てきて実行できなくなっていました
"that page does not exist." と表示されていますので、その account_name に対応するユーザがいない、ということかと思います。
まだ回答がついていません
会員登録して回答してみよう