How can I copy two tables from someones website to my website? WordPress

0 Votes
    1485 Views

I’m attempting to copy two tables from a specific website address and mimic them onto my website, but I’m unsure how to go about it.

I’ve looked into Simple-HTML-Dom, and I actually got it to work locally on my machine – to a certain extent. Although I have a couple of problems with this approach.

  1. Although I got it to work, it wasn’t perfect. I also dragged over some random text, and I copied across 5 tables – instead of the intended 2.

  2. I would rather use a different method – without using 3rd party scripts?

The tables that I’m attempting to copy can be located here:

https://www.gov.uk/government/publications/rates-and-allowances-monthly-euro-conversion-rates-for-calculating-duty/monthly-euro-conversion-rates-for-calculating-duty

I only want the tables containing 2017 and 2016 data (with the headings).

Rates for 2017

Table Headings

Table Contents

Rates for 2016

Table Headings

Table Contents

This would be for my WordPress website.
I don’t know if something could be programmed in PHP to achieve this, or another library that WordPress natively supports without the use of SQL / 3rd party scripts or anything like that.

Thank You

////

— HUGE UPDATE —

Ok, so I’ve been playing around and trying to debug the code, and I’m almost there!!

I’ve managed to copy over the first table only (second table will be easy).

The final part is trying to add classes to the createElements line? Is that possible??

Here’s my almost finished code.

<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]

I’m trying to change this line:

$newRow->appendChild($result->createElement("td", trim($cell->nodeValue)));

To this:

$newRow->appendChild($result->createElement("td class"roe"", trim($cell->nodeValue)));

But it’s not working?? The page just refuses to load? I guess it’s because of the double quotation.

Thanks

1

Answers


Please signup or login to answer this question.