ロイターRSS取得
ニュース取得にはRSSという方法もある
https://www.google.com/amp/www.algo-fx-blog.com/python-news-headlines-rss/amp/
によれば
feedparserライブラリでできるらしい
https://qiita.com/hann-solo/items/46d2bd25667618c36a5d
を参考に
ロイターRSSのURLを変更したらできたが
1 2 3 4 5 6 7 8 | # ニュースのヘッドライン print (reuters[ 'entries' ][ 0 ][ 'title' ]) # 公開日時 print (reuters[ 'entries' ][ 0 ][ 'published' ]) # URL print (reuters[ 'entries' ][ 0 ][ 'link' ]) |
でえらーになる
entries がうまく行かない
1 2 | # 取得したデータを確認 reuters |
で内容を確認
1 2 3 | # 公開日時 #print(reuters['entries'][0]['published']) print (reuters[ 'entries' ][ 0 ][ 'updated' ]) |
としたら表示できた
[/python]
# ニュースの取得件数の確認
len(reuters[‘entries’])
[/python]
で確認したら現在は10が限界
1 2 3 4 | # マーケットウォッチのデータを取得 market_df = pd.DataFrame(market[ 'entries' ]) market_df.head( 2 ) |
としたが見にくい
1 2 3 | # データを綺麗にする market_df = market_df[[ 'published' , 'title' ]] market_df.head() |
で見やすくなったが title が省略される
1 | market_df[[ 'title' ]] |
としても同じ
【Python】Pandasのデータフレームを省略せずに表示する方法を紹介!
を参考に省略せずに表示する
多分表示されなくてもデータの格納はされていると思う
https://qiita.com/kiddayo/items/e5ce519a234aff88af12
を参考に
1 2 | pd.set_option( 'display.max_rows' , 1500 ) pd.set_option( 'display.max_columns' , 4096 ) |
としたが
Summary 表示は変わらない
とりあえずは保留
またRSSだと
ニュース更新頻度に問題あるため
Twitter から取得する
すでに tweepy はインストール済
あとはツイート検索と取得
これもできたので
次はテキストのみ抽出する
Streamline でエラーになったが
https://teratail.com/questions/97557
を参考に
重複したdatetime を修正し
1 | new_date = datetime.strptime(i, "%d/%m/%Y" ).strftime( "%Y-%m-%d" ) |
としたら解決した
修正したので commit しようとしたら
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | Author identity unknown *** Please tell me who you are. Run git config --global user.email "you@example.com" git config --global user.name "Your Name" to set your account's default identity. Omit --global to set the identity only in this repository. fatal: unable to auto-detect email address |
となったので
1 2 3 | git config --global user.email "メルアド" git config --global user.name "snowpooll" |
を実行後
1 | git commit -m "Correction of datetime part" |
これで
1 | Git push origin main |
でOK
1 2 3 | for tweet in tweepy.Cursor(api.search_tweets, q = '"#雇用統計"' ).items( 10 ): print (tweet.text) |
とすれば
#雇用統計
を含むツイートが10件取得できる
1 | Tweet.text |
でツイート情報を取得できる
1 | Tweet.user |
ツイートしたユーザ情報にアクセス
1 | Tweet.user.name |
ユーザ名の取得
次に特定ユーザのツイート
1 2 3 | #if文にてRTとリプライを除外 [tweet.text for tweet in tweepy.Cursor(api.user_timeline, id = "Qiita" ).items( 10 ) if ( list (tweet.text)[: 2 ]! = [ 'R' , 'T' ]) & ( list (tweet.text)[ 0 ]! = '@' )] |
としたら
1 | Unexpected parameter id |
となるので id ではないかもしれない
https://toxublog.com/blog/get_tweet_tweepy/
を参考にDFにしてみた
必要なのは
ユーザ名
時刻
ツイート内容
これで一回経済指標で実験してみる
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #検索条件の設定 searchkey = '#雇用統計' item_num = 10 #検索条件を元にツイートを抽出 #tweets = tweepy.Cursor(api.search,q=searchkey,lang='ja').items(item_num) tweets = tweepy.Cursor(api.search_tweets,q = searchkey,lang = 'ja' ).items(item_num) tweet_data = [] for tweet in tweets: #tweet_dataの配列に取得したい情報を入れていく tweet_data.append([ tweet.text, tweet.user.screen_name, tweet.user.name ]) #取り出したデータをpandasのDataFrameに変換 #CSVファイルに出力するときの列の名前を定義 labels = [ 'ツイート内容' , 'ユーザID' , 'アカウント名' , ] #tweet_dataのリストをpandasのDataFrameに変換 tweet_df = pd.DataFrame(tweet_data,columns = labels) |
で大体近い感じ
あとは省略されている部分を調べる
Cvs では省略されていないので
そのままでも使えるはず
もしくはそのままでも使えるか調べる
経済指標アラートアカウント見たけど
アラートのみで実際の値はなし
Reutersとかのアカウントを使った方が正解かもしれない
とりあえず
アカウント名
ツイート内容が取得できればok
あとはここから絞り込み