public static async Task UpdateUsYahoo() {
int wait = 600;//銘柄間のスリープmsec
int retry = 3;//タイムアウト発生時のリトライ回数
Dictionary<int, string> dic = new Dictionary<int, string>() { { 301, "^DJI" }, { 302, "^IXIC" }, { 303, "^GSPC" } };
//Windows Chromeに偽装
string useragent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36";
using (HttpClient client = new HttpClient()) {
client.DefaultRequestHeaders.Add("User-Agent", useragent);
client.DefaultRequestHeaders.Add("Accept-Language", "ja-JP");
client.BaseAddress = new Uri("https://finance.yahoo.com/quote/";);
client.Timeout = new TimeSpan(0, 0, 0, 20);//秒
foreach (int code in dic.Keys) {
string symbol = System.Net.WebUtility.UrlEncode(dic[code]);
string url = $"{symbol}/history?p={symbol}";
string source = null;
int i = 0;
do {
try {
source = await client.GetStringAsync(url);
} catch (TaskCanceledException ex) {
//タイムアウトは継続してリトライ
} catch (Exception ex) { throw ex; }
i++;
} while (i < retry | source == null);
if (source == null) return;
Env.Frame.SetStatusBarText($"{url}", "");
string pattern = "<tr.*?<td.*?>([^<>]+)</.*?>([\\d,.]+)</.*?>([\\d,.]+)</.*?>([\\d,.]+)</.*?>([\\d,.]+)</.*?>([\\d,.]+)</.*?>([\\d,.]+)</";
MatchCollection mc = Regex.Matches(source, pattern, RegexOptions.IgnoreCase);