我想写一个显示最新美元货币的简单控制台应用程序。 数据将来自国家银行 - http://www.nbg.ge/rss.php
这是我的代码:
string data = string.Empty;
string url = "http://www.nbg.ge/rss.php";
string html = string.Empty;
string pattern = "USD</td>\r\n<td align=\"right\">(.*)</td>";
DateTime today = DateTime.Now;
data = today.Date.ToShortDateString();
HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
StreamReader myStreamReader = new StreamReader(myHttpWebResponse.GetResponseStream());
html = myStreamReader.ReadToEnd();
Match match = Regex.Match(html, pattern);
Console.WriteLine("{0} USD EXCHANGE RATE {1}.", data, match.Groups[1].ToString());
Console.ReadLine();
如何从网站获取美元数据? 感谢。
更新!
我改变了&#34;模式&#34;到string pattern = "<td.*?>(.*?)<\\/td>";
它在列表中显示第一个td项:
5/29/2016 USD兑换率AED。
答案 0 :(得分:0)
XML数据中存在错误。不得不使用正则表达式修复。在结束标记之前,图像标记缺少“/”。这是解析的结果。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.Text.RegularExpressions;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
const string url = "http://www.nbg.ge/rss.php";
public Form1()
{
InitializeComponent();
XElement rss = XElement.Load(url);
DataTable dt = new DataTable();
dt.Columns.Add("Currency", typeof(string));
dt.Columns.Add("Description", typeof(string));
dt.Columns.Add("Rate", typeof(decimal));
XElement description = rss.Descendants("item").FirstOrDefault().Descendants("description").FirstOrDefault();
string value = description.Value;
value = Regex.Replace(value, "(src=\\\"[^\\\"]+\\\")", "$1/");
XElement table = XElement.Parse(value);
foreach (XElement row in table.Descendants("tr"))
{
List<XElement> columns = row.Elements("td").ToList();
dt.Rows.Add(new object[] {(string)columns[0], (string)columns[1], (decimal)columns[2]});
}
dataGridView1.DataSource = dt;
}
}
}
答案 1 :(得分:0)
在PHP中,您可以使用CURL,SimpleXMLDocument,File和Array Helpers的组合轻松完成此操作,如下所示:
<?php
$defaults = array(
CURLOPT_URL => 'http://www.nbg.ge/rss.php',
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
);
$curl = curl_init();
curl_setopt_array($curl, $defaults);
$v = "";
// SHOULD ANY ERROR OCCUR, DISPLAY THE ERROR(S), DO GARBAGE COLLECTION & TERMINATE THE PROGRAM.
$curl_response = curl_exec($curl);
if( curl_errno($curl)){
var_dump( curl_error($curl));
curl_close($curl);
exit;
}else{
// PROCESS THE RSS FEED...
if($curl_response){
$sxElem = new SimpleXMLElement($curl_response);
$xml = simplexml_load_string($curl_response, 'SimpleXMLElement', LIBXML_NOCDATA); //LIBXML_NOCDATA LIBXML_NOWARNING
foreach($xml as $key=>$data){
if($a = $data->item){
/**@var SimpleXMLElement $desc */
$desc = $a->description;
$v = print_r($a->description, true);
}
}
}
}
// SAVE THE DATA TO A TEXT FILE TO BE READ LINE BY LINE LATER...
file_put_contents("data.txt", $v);
// GET THE DATA FROM THE TEXT FILE AND THEN EXPLODE TO AN ARRAY
$data = file_get_contents("_data.txt");
$arrData = explode("\n", $data);
$countryCodes = array();
$exchangeRates = array();
// LOOP THROUGH EACH LINE & SORT OUT THE DATA YOU NEED...
foreach($arrData as $lineNum=>$lineData){
$countryCode = null;
if( preg_match("#(<td>)([A-Z]{3})(<\/td>)#", $lineData, $matches)){
list(, $n1, $countryCode, $n2) = $matches;
if(!array_key_exists($countryCode, $exchangeRates)){
$countryCodes[] = $countryCode;
}
}
if( preg_match("#(<td>)([0-9\.]*)(<\/td>)#", $lineData, $matches2)){
list(, $n1, $rate, $n2) = $matches2;
if($rate != "0.0000"){
$exchangeRates[] = $rate;
}
}
}
$arrExchange = array_combine($countryCodes, $exchangeRates);
$jsonExchangeRate = json_encode($arrExchange);
var_dump($arrExchange);
var_dump($jsonExchangeRate);
第一个var_dump的结果是:
array (size=43)
'AED' => string '5.8432' (length=6)
'AMD' => string '4.4921' (length=6)
'AUD' => string '1.5491' (length=6)
'AZN' => string '1.4388' (length=6)
'BGN' => string '1.2263' (length=6)
'BRL' => string '0.5989' (length=6)
'BYR' => string '1.0898' (length=6)
'CAD' => string '1.6465' (length=6)
'CHF' => string '2.1662' (length=6)
'CNY' => string '3.2708' (length=6)
'CZK' => string '0.8875' (length=6)
'DKK' => string '3.2253' (length=6)
'EGP' => string '2.4197' (length=6)
'EUR' => string '2.3985' (length=6)
'GBP' => string '3.1438' (length=6)
'HKD' => string '2.7637' (length=6)
'HUF' => string '0.7631' (length=6)
'ILS' => string '5.5821' (length=6)
'INR' => string '3.2016' (length=6)
'IRR' => string '0.7082' (length=6)
'ISK' => string '1.7194' (length=6)
'JPY' => string '1.9585' (length=6)
'KGS' => string '3.1414' (length=6)
'KRW' => string '1.8198' (length=6)
'KWD' => string '7.1039' (length=6)
'KZT' => string '0.6392' (length=6)
'MDL' => string '1.0714' (length=6)
'NOK' => string '2.5844' (length=6)
'NZD' => string '1.4469' (length=6)
'PLN' => string '5.4544' (length=6)
'QAR' => string '5.8943' (length=6)
'RON' => string '5.3217' (length=6)
'RSD' => string '1.9438' (length=6)
'RUB' => string '3.2531' (length=6)
'SEK' => string '2.5872' (length=6)
'SGD' => string '1.5619' (length=6)
'TJS' => string '2.7273' (length=6)
'TMT' => string '6.1317' (length=6)
'TRY' => string '0.7288' (length=6)
'UAH' => string '0.8538' (length=6)
'USD' => string '2.1461' (length=6)
'UZS' => string '0.7351' (length=6)
'ZAR' => string '1.3802' (length=6)
JSON等效的结果是:
'{"AED":"5.8432","AMD":"4.4921","AUD":"1.5491","AZN":"1.4388","BGN":"1.2263","BRL":"0.5989","BYR":"1.0898","CAD":"1.6465","CHF":"2.1662","CNY":"3.2708","CZK":"0.8875","DKK":"3.2253","EGP":"2.4197","EUR":"2.3985","GBP":"3.1438","HKD":"2.7637","HUF":"0.7631","ILS":"5.5821","INR":"3.2016","IRR":"0.7082","ISK":"1.7194","JPY":"1.9585","KGS":"3.1414","KRW":"1.8198","KWD":"7.1039","KZT":"0.6392","MDL":"1.0714","NOK":"2.5844","NZD":"1.4469","PLN":"5.4544","QAR":"5.8943","RON":"5.3217","RSD":"1.9438","RUB":"3.2531","'... (length=646)