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

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

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

Twitterは、140文字以内の「ツイート」と呼ばれる短文を投稿できるサービスです。Twitter上のほぼ全ての機能に対応するAPIが存在し、その関連サービスが多く公開されています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

2回答

1115閲覧

tweepy error code

python

総合スコア5

Twitter

Twitterは、140文字以内の「ツイート」と呼ばれる短文を投稿できるサービスです。Twitter上のほぼ全ての機能に対応するAPIが存在し、その関連サービスが多く公開されています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2022/01/30 04:06

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

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

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

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

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

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

guest

回答2

0

ベストアンサー

Tweepy 4.5.0 の user_timeline メソッドの API を見てみますと、以下の様になっていて positional parameter がありません(keyword のみ)。

Tweepy: api.py

python

1 def user_timeline(self, **kwargs): 2 """user_timeline(*, user_id, screen_name, since_id, count, max_id, \ 3 trim_user, exclude_replies, include_rts) 4 5 Returns the 20 most recent statuses posted from the authenticating user 6 or the user specified. It's also possible to request another user's 7 timeline via the id parameter.

なので、以下の様になるかと思いますが、上述の API には page キーワードがありません。

python

1#user_id = api.user_timeline(account_name, count=1, page=0)[0].user.id 2user_id = api.user_timeline(user_id=account_name, count=1, page=0)[0].user.id

投稿2022/01/30 05:43

melian

総合スコア19712

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

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

0

ありがとうございました。
変えてみたところ、うまく実行することができました

投稿2022/01/30 08:10

python

総合スコア5

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問