我想从这个link获取货币表数据,但我无法获得表格中的数据......我尝试了不同的方法,但都是徒劳的
我正在使用的库:https://sourceforge.net/projects/simplehtmldom/
这是php代码:
require "simple_html_dom.php";
$html = file_get_html("http://www.cibeg.com/English/Pages/CIBCurrencies.aspx");
$c = $html->find("#divCurrTableContainer",0)->innertext;
echo $c;
答案 0 :(得分:1)
:
您的代码应该与此选择器一起使用:#divCurrTableContainer > table
$c=$html->find('#selector_here');
foreach($c as $element)
echo $element->innerText();
正如@nogad指出的那样,使用API进行实时数据...随着时间的推移,报废不会对这类数据有所帮助。
一些参考文献:
这里有一些可能对fixer.io有帮助的API并问你的谷歌朋友qry:Live Currency API
答案 1 :(得分:0)
以下是其网站用于获取该表中信息的代码:
$.ajax({
type: "POST",
url: '/_layouts/15/LINKDev.CIB.CurrenciesFunds/FundsCurrencies.aspx/GetCurrencies',
async: true,
data: "{'lang':'" + document.getElementById("ctl00_ctl48_g_5d7fc52f_a66d_4aa2_8d6c_c01fb4b38cb2_hdnLang").value + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
if (msg.d != null && msg.d.length > 0) {
var contentHTML = "<table class='currTable' cellspacing='0' rules='all' style='border-width:0px;border-collapse:collapse;'>"
+ "<tbody><tr class='currHeaderRow'>"
+ "<th scope='col'>" + document.getElementById("ctl00_ctl48_g_5d7fc52f_a66d_4aa2_8d6c_c01fb4b38cb2_hdnCurrency").value + "</th><th scope='col'>" + document.getElementById("ctl00_ctl48_g_5d7fc52f_a66d_4aa2_8d6c_c01fb4b38cb2_hdnBuy").value + "</th><th scope='col'>" + document.getElementById("ctl00_ctl48_g_5d7fc52f_a66d_4aa2_8d6c_c01fb4b38cb2_hdnSell").value + "</th>"
+ "</tr>";
for (var i = 0; i < msg.d.length; i++) {
if (msg.d[i].CurrencyID.length > 0) {
contentHTML += "<tr class='currRow'>"
+ "<td>" + msg.d[i].CurrencyID + "</td><td>" + msg.d[i].BuyRate + "</td><td class='lastCell'>" + msg.d[i].SellRate + "</td>"
+ "</tr>";
}
}
contentHTML += "</tbody></table>";
$("#divCurrTableContainer").html(contentHTML);
if ($(".bannerElements").length > 0)
FixCurrenciesRatesScroll();
}
},
error: function (msg) {
}
});
在php中,我们可以使用curl复制此请求:
<?php
$ch = curl_init('http://www.cibeg.com/_layouts/15/LINKDev.CIB.CurrenciesFunds/FundsCurrencies.aspx/GetCurrencies');
$data = array("lang" => "en");
$data_string = json_encode($data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
var_dump($server_output);
?>
将其粘贴到http://phpfiddle.org/并看到你得到的结果,你必须用php解析