質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

0回答

384閲覧

引数の数が違うというエラーが治りません

sugano

総合スコア0

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

1クリップ

投稿2022/06/25 02:30

編集2022/06/26 02:31

twitterで自分の固定ツイートをリツイートした人に自動でDmを送るpythonのプログラムを作ったのですが

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 # sleepでとり得る最小値 19random_max_time = 400 # sleepでとり得る最大値 20 21endpoint = 'https://api.twitter.com/2/users?ids=----- 22 23params = {'expansions': 'pinned_tweet_id'} 24 25res = twitter.get(endpoint, params=params) 26 27dic_me = json.loads(res.text) 28 29print(dic_me['data'][0]['pinned_tweet_id']) 30 31print(dic_me['includes']['tweets'][0]['text']) 32 33tweet_id = dic_me['data'][0]['pinned_tweet_id'] 34 35endpoint = 'https://api.twitter.com/1.1/statuses/retweeters/ids.json?id=' + 36tweet_id + '&stringify_ids=true' 37 38res = twitter.get(endpoint) 39 40dic_users = json.loads(res.text) 41 42for num in range(len(dic_users['ids'])): 43account_name = dic_users['ids'][num] 44user_id = api.user_timeline(account_name, count=1)[0].user.id 45try: 46api.send_direct_message(recipient_id=user_id, text=‘hello) 47print('メッセージを送信しました:', account_name) 48except Exception as e: 49print("Error!!!", e) 50except StopIteration: 51continue 52sleep_random_time = random.randint(random_min_time, random_max_time) 53print(account_name, ': ', ' 次のDM送信まで', sleep_random_time, '秒待ちます') 54time.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をつけてみても直りません

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

melian

2022/06/25 02:48

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 を試してみて下さい。
y_waiwai

2022/06/25 08:15

このままではコードが読めないので、質問を編集し、</>(コードの挿入)ボタンを押し、出てくる’’’の枠の中にコードを貼り付けてください
sugano

2022/06/26 03:17

@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.'}] というのが出てきて実行できなくなっていました
melian

2022/06/26 03:27

"that page does not exist." と表示されていますので、その account_name に対応するユーザがいない、ということかと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問