python で今日の日付を dd/mm/YY で取得 その2
ほとんどの場合は
yymmdd だけど
investpy で指定するときに
ddmmyy で期間を取得することになる
今日の日付を[yyyymmdd]形式の文字列で取得する。
を参考に
1 2 | today = datetime.date.today().strftime( '%Y%d%m' ) today |
とすると
‘20210409’
となる
1 2 | today = datetime.date.today().strftime( '%d/%m/%Y' ) today |
とすれば
’04/09/2021′
となる
【Python】investpyを使ったInvesting.comからのデータ取得方法
で
経済指標カレンダーも investpy で取得可能
1 2 3 4 5 6 7 | calendar = investpy.news.economic_calendar(from_date = '01/01/2021' ,to_date = today, countries = [ 'united states' , 'germany' , 'japan' ]) # countriesでの国指定は省略可能。 # 重要度の高・中度の予定 important = investpy.news.economic_calendar(from_date = '01/01/2021' ,to_date = today, countries = [ 'united states' ],importances = [ 'high' , 'medium' ]) |
で取得ができる
あとは
important
や
calendar
で表示ができるけど
日時を毎回直接打つのはミスするので別の方法で取得
を参考に今月と月末を取得して指定するようにする
1 2 | from datetime import datetime, date, timedelta from dateutil.relativedelta import relativedelta |
で必要なライブラリをインポート
1 2 3 4 5 6 7 | # 月初(今月の1日にする) first_day = (datetime.today() + relativedelta(day = 1 )).strftime( '%d/%m/%Y' ) # 月末 (来月の1日に合わせて1日分戻る) last_day = (datetime.today() + relativedelta(months = + 1 ,day = 1 ,days = - 1 )).strftime( '%d/%m/%Y' ) # 1週間後 week_later = (datetime.today() + relativedelta(weeks = + 1 )).strftime( '%d/%m/%Y' ) |
で
月末、月始の日付が取得できる
あとは
1 2 3 4 5 6 7 8 | # 今月の重要度の高・中度の予定 important = investpy.news.economic_calendar(from_date = first_day,to_date = last_day, countries = [ 'united states' ],importances = [ 'high' , 'medium' ]) # 1週間の重要度の高・中度の予定 important = investpy.news.economic_calendar(from_date = today,to_date = week_later, countries = [ 'united states' ],importances = [ 'high' , 'medium' ]) |
で指定して
経済指標カレンダーの取得が可能になる