【发布时间】:2017-11-01 15:29:40
【问题描述】:
我正在尝试从特定网站地址复制两个表格并将它们模仿到我的网站上,但我不确定如何去做。
我研究了 Simple-HTML-Dom,实际上我让它在我的机器上本地工作 - 在一定程度上。虽然我对这种方法有一些问题。
虽然我让它工作了,但它并不完美。我还拖动了一些随机文本,并复制了 5 个表格 - 而不是预期的 2 个。
我宁愿使用不同的方法 - 不使用 3rd 方脚本?
我尝试复制的表可以位于此处:
我只想要包含 2017 年和 2016 年数据的表格(带有标题)。
2017 年费率
表格标题
表格内容
2016 年费率
表格标题
表格内容
这将用于我的 Wordpress 网站。 我不知道是否可以在 PHP 中编写一些东西来实现这一点,或者 Wordpress 本身支持的另一个库,而不使用 SQL / 3rd 方脚本或类似的东西。
谢谢
////
--- 巨大的更新 ---
好的,所以我一直在玩并尝试调试代码,我快到了!!
我已经设法只复制了第一个表(第二个表会很容易)。
最后一部分是尝试将类添加到 createElements 行?这可能吗??
这是我几乎完成的代码。
<h1 class="roeheader">Monthly Industrial Euro Rate:</h1>
[insert_php]
$url = "https://www.gov.uk/government/publications/rates-and-allowances-monthly-euro-conversion-rates-for-calculating-duty/monthly-euro-conversion-rates-for-calculating-duty";
$html = file_get_contents($url);
libxml_use_internal_errors(true);
$doc = new \DOMDocument();
if($doc->loadHTML($html))
{
$result = new \DOMDocument();
$result->formatOutput = true;
$table = $result->appendChild($result->createElement("table"));
$thead = $table->appendChild($result->createElement("thead"));
$tbody = $table->appendChild($result->createElement("tbody"));
$xpath = new \DOMXPath($doc);
$newRow = $thead->appendChild($result->createElement("tr"));
foreach($xpath->query("//table[1]/thead/tr/th[position()>0]") as $header)
{
$newRow->appendChild($result->createElement("th", trim($header->nodeValue)));
}
foreach($xpath->query("//table[1]/tbody/tr") as $row)
{
$newRow = $tbody->appendChild($result->createElement("tr"));
foreach($xpath->query("./td[position()>0 and position()<5]", $row) as $cell)
{
$newRow->appendChild($result->createElement("td", trim($cell->nodeValue)));
}
}
echo $result->saveXML($result->documentElement);
}
[/insert_php]
我正在尝试更改这一行:
$newRow->appendChild($result->createElement("td", trim($cell->nodeValue)));
到这里:
$newRow->appendChild($result->createElement("td class"roe"", trim($cell->nodeValue)));
但它不工作?页面只是拒绝加载?我猜是因为双引号。
谢谢
【问题讨论】:
-
“我让它工作了,它并不完美” - 向我们展示你的尝试,我们或许可以在这部分为你提供帮助。目前,这个问题太宽泛了。
-
Magnus,我可以让你看看我做了什么……但这不是我想走的路。我不想使用第 3 方脚本。我想使用更原生的东西。
-
Nic3500,这看起来是一个可行的解决方案。但是我似乎无法让我的代码正确以正确显示表格?我将在我当前的代码下方发布。如果有人可以看看并希望能帮助我让它正常工作。谢谢
-
抱歉,仍在尝试掌握 stackoverflow...我已经添加了我在原始问题帖子中尝试使用的代码...