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

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

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

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

Twitter

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

Q&A

解決済

2回答

2921閲覧

pythonによるツイッターの投稿分析

hide09090909

総合スコア68

Python 3.x

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

Twitter

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

0グッド

0クリップ

投稿2022/01/04 12:45

pythonで任意の単語を指定するとツイッターからその単語が含まれる投稿情報をcsvに出力するコードを作成したいです。
下記サイトを参考にAPIの使用承認を取り、作成したのですが、■エラー内容 に記載のエラーが表示され実行できません。
どなたか原因わかりますでしょうか?また、その他動作のために良くない記載箇所があれば教えていただけますと幸いです。
https://toxublog.com/blog/get_tweet_tweepy/#ok

#ライブラリのインポート import tweepy from datetime import datetime,timezone import pytz import pandas as pd #Twitterの認証 api_key = "xxxxxxxxxxx" api_secret = "xxxxxxxxxxx" access_key = "xxxxxxxxxxx" access_secret = "xxxxxxxxxxx" auth = tweepy.OAuthHandler(api_key, api_secret) auth.set_access_token(access_key, access_secret) api = tweepy.API(auth) #検索条件の設定 searchkey = 'プログラミング' item_num = 10 #検索条件を元にツイートを抽出 tweets = tweepy.Cursor(api.search,q=searchkey,lang='ja').items(item_num) #関数: UTCをJSTに変換する def change_time_JST(u_time): #イギリスのtimezoneを設定するために再定義する utc_time = datetime(u_time.year, u_time.month,u_time.day, \ u_time.hour,u_time.minute,u_time.second, tzinfo=timezone.utc) #タイムゾーンを日本時刻に変換 jst_time = utc_time.astimezone(pytz.timezone("Asia/Tokyo")) # 文字列で返す str_time = jst_time.strftime("%Y-%m-%d_%H:%M:%S") return str_time #抽出したデータから必要な情報を取り出す #取得したツイートを一つずつ取り出して必要な情報をtweet_dataに格納する tweet_data = [] for tweet in tweets: #ツイート時刻とユーザのアカウント作成時刻を日本時刻にする tweet_time = change_time_JST(tweet.created_at) create_account_time = change_time_JST(tweet.user.created_at) #tweet_dataの配列に取得したい情報を入れていく tweet_data.append([ tweet.id, tweet_time, tweet.text, tweet.favorite_count, tweet.retweet_count, tweet.user.id, tweet.user.screen_name, tweet.user.name, tweet.user.description, tweet.user.friends_count, tweet.user.followers_count, create_account_time, tweet.user.following, tweet.user.profile_image_url, tweet.user.profile_background_image_url, tweet.user.url ]) #取り出したデータをpandasのDataFrameに変換 #CSVファイルに出力するときの列の名前を定義 labels=[ 'ツイートID', 'ツイート時刻', 'ツイート内容', 'いいね数', 'リツイート数', 'ID', 'ユーザID', 'アカウント名', '自己紹介文', 'フォロー数', 'フォロワー数', 'アカウント作成日時', '自分がフォローしているか?', 'アイコン画像URL', 'ヘッダー画像URL', 'WEBサイト' ] #tweet_dataのリストをpandasのDataFrameに変換 df = pd.DataFrame(tweet_data,columns=labels) #CSVファイルに出力する #CSVファイルの名前を決める file_name='tweet_data.csv' #CSVファイルを出力する df.to_csv(file_name,encoding='utf-8-sig',index=False)

■エラー内容

line 18, in <module> tweets = tweepy.Cursor(api.search,q=searchkey,lang='ja').items(item_num) AttributeError: 'API' object has no attribute 'search'

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

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

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

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

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

guest

回答2

0

ベストアンサー

どうやらメソッドの名前が変更された模様です。

Changelog Version 4.0.0

API

Rename API and models methods

API.search -> API.search_tweets (7fac253)

Rename API.search to API.search_tweets · tweepy/tweepy@7fac253

投稿2022/01/04 13:10

melian

総合スコア19825

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

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

hide09090909

2022/01/04 14:41

ご回答ありがとうございます。いただいた通りに修正したところ下記のエラーになりました。 必要なアカウント権限が不足しているんですかね??? line 18, in <module> tweets = tweepy.Cursor(api.search,q=searchkey,lang='ja').items(item_num) AttributeError: 'API' object has no attribute 'search' (base) masuyamatakahironoMacBook-Air:ツイッター masuyamatakahiro$ python ツイッター.py Traceback (most recent call last): File "/Users/masuyamatakahiro/Desktop/副業/ツイッター/ツイッター.py", line 32, in <module> for tweet in tweets: File "/Users/masuyamatakahiro/opt/anaconda3/lib/python3.9/site-packages/tweepy/cursor.py", line 86, in __next__ return self.next() File "/Users/masuyamatakahiro/opt/anaconda3/lib/python3.9/site-packages/tweepy/cursor.py", line 286, in next self.current_page = next(self.page_iterator) File "/Users/masuyamatakahiro/opt/anaconda3/lib/python3.9/site-packages/tweepy/cursor.py", line 86, in __next__ return self.next() File "/Users/masuyamatakahiro/opt/anaconda3/lib/python3.9/site-packages/tweepy/cursor.py", line 167, in next data = self.method(max_id=self.max_id, parser=RawParser(), *self.args, **self.kwargs) File "/Users/masuyamatakahiro/opt/anaconda3/lib/python3.9/site-packages/tweepy/api.py", line 33, in wrapper return method(*args, **kwargs) File "/Users/masuyamatakahiro/opt/anaconda3/lib/python3.9/site-packages/tweepy/api.py", line 46, in wrapper return method(*args, **kwargs) File "/Users/masuyamatakahiro/opt/anaconda3/lib/python3.9/site-packages/tweepy/api.py", line 1268, in search_tweets return self.request( File "/Users/masuyamatakahiro/opt/anaconda3/lib/python3.9/site-packages/tweepy/api.py", line 259, in request raise Forbidden(resp) tweepy.errors.Forbidden: 403 Forbidden 453 - You currently have Essential access which includes access to Twitter API v2 endpoints only. If you need access to this endpoint, you’ll need to apply for Elevated access via the Developer Portal. You can learn more here: https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api#v2-access-leve
melian

2022/01/04 14:57

そのドキュメントを眺めると、エラーメッセージにも書かれてある通り Essential Access の場合は Twitter API v2 しか使えない様ですね。なおかつ、「Access to full-archive search Tweets」にバツ印が付いていますので、今回の様な処理は難しいのではないでしょうか。
guest

0

api.searchapi.search_tweetsに修正してみてはいかがでしょうか?

https://qiita.com/t140513/items/a2ef7d77fcb0ade7944e

投稿2022/01/04 13:02

Supernove

総合スコア1154

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

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

hide09090909

2022/01/04 14:41

ご回答ありがとうございます。いただいた通りに修正したところ下記のエラーになりました。 必要なアカウント権限が不足しているんですかね??? line 18, in <module> tweets = tweepy.Cursor(api.search,q=searchkey,lang='ja').items(item_num) AttributeError: 'API' object has no attribute 'search' (base) masuyamatakahironoMacBook-Air:ツイッター masuyamatakahiro$ python ツイッター.py Traceback (most recent call last): File "/Users/masuyamatakahiro/Desktop/副業/ツイッター/ツイッター.py", line 32, in <module> for tweet in tweets: File "/Users/masuyamatakahiro/opt/anaconda3/lib/python3.9/site-packages/tweepy/cursor.py", line 86, in __next__ return self.next() File "/Users/masuyamatakahiro/opt/anaconda3/lib/python3.9/site-packages/tweepy/cursor.py", line 286, in next self.current_page = next(self.page_iterator) File "/Users/masuyamatakahiro/opt/anaconda3/lib/python3.9/site-packages/tweepy/cursor.py", line 86, in __next__ return self.next() File "/Users/masuyamatakahiro/opt/anaconda3/lib/python3.9/site-packages/tweepy/cursor.py", line 167, in next data = self.method(max_id=self.max_id, parser=RawParser(), *self.args, **self.kwargs) File "/Users/masuyamatakahiro/opt/anaconda3/lib/python3.9/site-packages/tweepy/api.py", line 33, in wrapper return method(*args, **kwargs) File "/Users/masuyamatakahiro/opt/anaconda3/lib/python3.9/site-packages/tweepy/api.py", line 46, in wrapper return method(*args, **kwargs) File "/Users/masuyamatakahiro/opt/anaconda3/lib/python3.9/site-packages/tweepy/api.py", line 1268, in search_tweets return self.request( File "/Users/masuyamatakahiro/opt/anaconda3/lib/python3.9/site-packages/tweepy/api.py", line 259, in request raise Forbidden(resp) tweepy.errors.Forbidden: 403 Forbidden 453 - You currently have Essential access which includes access to Twitter API v2 endpoints only. If you need access to this endpoint, you’ll need to apply for Elevated access via the Developer Portal. You can learn more here: https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api#v2-access-leve
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問