-
schedule
を用いたPython定期処理についてメモする。 - News APIをscheduleを用いて、定期実行する。
事前準備
-
schedule
インストールpip install schedule
-
News API 利用準備
-
APIキー取得
* 公式サイトにアクセスし、「Get API Key」から必要情報を入力し、APIキーを取得する。 -
Python用APIクライアント インストール
pip install newsapi-python
-
コード
-
run.py
- News APIを3時間ごとに実行させる。
from newsapi import NewsApiClient from schedule import every, repeat, run_pending import time import datetime # APIクライアントセットアップ newsapi = NewsApiClient(api_key='{APIキー}}') # News 取得用ジョブ # 3時間毎にjobを実行 @repeat(every(3).hours) def job(): dt_now = datetime.datetime.now() print('/////////////////////////////////////////////////////////////////////') print(f'Execution Time : {dt_now}') # News Headline 取得 # 例:"bitcoin"で記事検索 top_headlines = newsapi.get_top_headlines(q='bitcoin', category='business', language='en', country='us') print(f'Num of Articles : {top_headlines.get("totalResults")}') num = 1 for article in top_headlines.get('articles'): print( f'No.{num} : {article.get("publishedAt")} / {article.get("title")} / {article.get("author")} / {article.get("url")}') num += 1 print('/////////////////////////////////////////////////////////////////////') while True: run_pending() time.sleep(1)
動作確認
-
実行
python run.py ///////////////////////////////////////////////////////////////////// Execution Time : 2021-07-22 14:03:04.950817 Num of Articles : 10 No.1 : 2021-07-22T02:13:00Z / Tesla will 'most likely' restart accepting bitcoin as payments, says Musk - Reuters / Noor Zainab Hussain,Nivedita Balu / https://www.reuters.com/business/autos-transportation/tesla-will-most-likely-restart-accepting-bitcoin-payments-says-musk-2021-07-21/ No.2 : 2021-07-22T01:58:00Z / Dow Jones Futures: Market Rally Still Lacks This; Bitcoin Rebounds, Square, Tesla-Rival Li Auto Flash Buy Signs - Investor's Business Daily / Investor's Business Daily / https://www.investors.com/market-trend/stock-market-today/dow-jones-futures-market-rally-lacks-this-bitcoin-rebounds-square-tesla-rival-li-auto-flash-buy-signs/ No.3 : 2021-07-22T00:44:13Z / SpaceX owns Bitcoin, Elon Musk and Nic Carter believe BTC is becoming greener - Cointelegraph / Brian Quarmby / https://cointelegraph.com/news/spacex-owns-bitcoin-elon-musk-and-nic-carter-believe-btc-is-becoming-greener No.4 : 2021-07-21T18:04:45Z / DraftKings Launching An NFT Ecosystem & Exchange - bitcoinist.com / Taylor Scott / https://bitcoinist.com/draftkings-launching-an-nft-ecosystem-exchange/ No.5 : 2021-07-21T17:37:47Z / Price analysis 7/21: BTC, ETH, BNB, ADA, XRP, DOGE, DOT, UNI, BCH, LTC - Cointelegraph / Rakesh Upadhyay / https://cointelegraph.com/news/price-analysis-7-21-btc-eth-bnb-ada-xrp-doge-dot-uni-bch-ltc No.6 : 2021-07-21T17:12:37Z / ‘PlugWalkJoe’ arrested in connection with 2020 hack of famous Twitter accounts - The Verge / Ian Carlos Campbell / https://www.theverge.com/2021/7/21/22587022/twitter-hack-2020-man-arrested-uk-spain No.7 : 2021-07-21T16:34:21Z / Major Crypto Mining Company Core Scientific Going Public on Nasdaq With $4.3 Billion Valuation – Bitcoin News - Bitcoin News / None / https://news.bitcoin.com/major-crypto-mining-company-core-scientific-going-public-on-nasdaq-with-4-3-billion-valuation/ No.8 : 2021-07-21T16:31:19Z / Bitcoin price hits $32K but derivatives metrics still show signs of weakness - Cointelegraph / Marcel Pechman / https://cointelegraph.com/news/bitcoin-price-hits-32k-but-derivatives-metrics-still-show-signs-of-weakness No.9 : 2021-07-21T11:44:42Z / EU Proposes Law to 'Ensure Full Traceability' of Crypto Transfers, Ban Anonymous Wallets – Regulation Bitcoin News - Bitcoin News / None / https://news.bitcoin.com/eu-proposes-law-to-ensure-full-traceability-of-crypto-transfers-ban-anonymous-wallets/ No.10 : 2021-07-20T19:09:47Z / Bitcoin Volume Observes Yearly Lows As Volatility Declines Further - bitcoinist.com / Hououin Kyouma / https://bitcoinist.com/bitcoin-volume-observes-yearly-lows-as-volatility-declines-further/ /////////////////////////////////////////////////////////////////////