pythonでリツイートした人にDM送信するコードを作成したいです。
APIの使用承認を取り、作成したのですが、■エラー内容 に記載のエラーが表示され実行できません。
どなたか原因わかりますでしょうか?また、その他動作のために良くない記載箇所があれば教えていただけますと幸いです。
作成したコード
python
1import json 2import time 3import random 4from requests_oauthlib import OAuth1Session 5import tweepy 6 7api_key = '-----------------------' 8api_secret = '-----------------------' 9access_key = '-----------------------' 10access_secret = '-----------------------' 11 12twitter = OAuth1Session(api_key,api_secret,access_key,access_secret) 13 14auth=tweepy.OAuthHandler(api_key, api_secret) 15auth.set_access_token(access_key, access_secret) 16api = tweepy.API(auth) 17 18random_min_time = 300 19random_max_time = 400 20 21endpoint = 'https://api.twitter.com/2/users?ids=1453019859307040771' 22params = {'expansions':'pinned_tweet_id'} 23res = twitter.get(endpoint, params = params) 24dic_me = json.loads(res.text) 25print(dic_me['data'][0]['pinned_tweet_id']) 26print(dic_me['includes']['tweets'][0]['text']) 27tweet_id=dic_me['data'][0]['pinned_tweet_id'] 28endpoint = 'https://api.twitter.com/1.1/statuses/retweeters/ids.json?id=' + tweet_id + '&stringify_ids=true' 29res = twitter.get(endpoint) 30dic_users = json.loads(res.text) 31 32for num in range(len(dic_users['ids'])): 33 account_name = dic_users['ids'][num] 34 user_id = api.user_timeline(account_name, count=1, page=0)[0].user.id 35 try: 36 api.send_direct_message(recipient_id=user_id,text='おはようございます。') 37 print('メッセージを送信しました:', account_name) 38 except Exception as reason: 39 print("Error!!!", reason) 40 except StopIteration: 41 continue 42 sleep_random_time = random.randint(random_min_time,random_max_time) 43 44 print(account_name,': ',' 次のDM送信まで' ,sleep_random_time,'秒待ちます') 45 time.sleep(sleep_random_time)
エラーコード
python
1Traceback (most recent call last): 2 File "c:\Users\user\Desktop\automatic_DM.py", line 35, in <module> 3 user_id = api.user_timeline(account_name, count=1, page=0)[0].user.id 4 File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\tweepy\api.py", line 33, in wrapper 5 return method(*args, **kwargs) 6 File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\tweepy\api.py", line 46, in wrapper 7 return method(*args, **kwargs) 8TypeError: API.user_timeline() takes 1 positional argument but 2 were given
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。