谷歌货币转换器
本文关键字:转换器 货币 谷歌 | 更新日期: 2023-09-27 18:08:03
我在过去几年里使用过这个代码,但是谷歌似乎已经改变了他们的一些链接。由于某种原因,我得到了这个错误消息:
"输入字符串格式不正确。"
decimal rate = System.Convert.ToDecimal(match.Groups[1].Value);
我代码:try
{
WebClient web = new WebClient();
string url = string.Format("https://www.google.com/finance/converter?a={2}&from={0}&to={1}", fromCurrency.ToUpper(), toCurrency.ToUpper(), amount);
string response = web.DownloadString(url);
Regex regex = new Regex("rhs: '''"(''d*.''d*)");
Match match = regex.Match(response);
decimal rate = System.Convert.ToDecimal(match.Groups[1].Value);
return rate;
}
catch
{
return 0;
}
你可能不喜欢这种方法,但它可以完成工作。
WebClient web = new WebClient();
string url = string.Format("https://www.google.com/finance/converter?a={2}&from={0}&to={1}", fromCurrency.ToUpper(), toCurrency.ToUpper(), amount);
string response = web.DownloadString(url);
var split = response.Split((new string[] { "<span class=bld>"}),StringSplitOptions.None);
var value = split[1].Split(' ')[0];
decimal rate = decimal.Parse(value,CultureInfo.InvariantCulture);