C#からPythonでWebApiのdataを取得する
ProcessのOutputDataReceivedとDataReceivedEventHandlerを使ってデータを受信します
using System;
using System.Diagnostics;
Process process = new Process();
process.StartInfo.FileName = "/usr/bin/python3.6";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.EnableRaisingEvents = true;
//パスを指定する
process.StartInfo.Arguments = "/test.py 7203";
process.Exited += (s, evt) => {
process?.Dispose();
};
process.OutputDataReceived += new DataReceivedEventHandler((_sender, _e) =>
{
if (!String.IsNullOrEmpty(_e.Data))
{
Console.WriteLine(_e.Data);
}
}
);
process.Start();
process.BeginOutputReadLine();
test.py
import json
import sys
import investpy
# pip install investpy
args = sys.argv
#トヨタ自動車 7203
code = args[0]
stock_data = investpy.get_stock_historical_data(as_json=True,stock=code, country='japan', from_date='01/02/2021', to_date='28/03/2021')
print(stock_data)