Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

php - How to parse this table and extract data from it?

I have the following table: http://www.nbs.rs/kursnaListaModul/srednjiKurs.faces?lang=lat

It is a currency exchange list and I need to extract some data from it. On left side of the table are currency ID numbers. Would it be possible to extract data from specified rows based on their IDs?

For example, from the table above, I want to extract currencies with IDs 978, 203, and 348.

Output should be:

  • EUR 104,2182
  • CZK 4,2747
  • HUF 38,7919

By looking at similar examples here, I came up with this: http://pastebin.com/hFZs1H7C

I need somehow to detect IDs and the print proper values... I'm noob when it comes to programming and I need your help.

<?php
$data = file_get_contents('http://www.nbs.rs/kursnaListaModul/srednjiKurs.faces?lang=lat');

$dom = new domDocument;

@$dom->loadHTML($data);
$dom->preserveWhiteSpace = false;
$tables = $dom->getElementsByTagName('table');

$rows = $tables->item(1)->getElementsByTagName('tr');

foreach ($rows as $row) {
        $cols = $row->getElementsByTagName('td');
                foreach ($cols as $col) {

                                echo $col;

                }
}

?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Collecting the table data as array for later usage:

$dom = new DomDocument;
$dom->loadHtmlFile('http://www.nbs.rs/kursnaListaModul/srednjiKurs.faces?lang=lat');

$xpath = new DomXPath($dom);

// collect header names
$headerNames = array();
foreach ($xpath->query('//table[@id="index:srednjiKursLista"]//th') as $node) {
    $headerNames[] = $node->nodeValue;
}

// collect data
$data = array();
foreach ($xpath->query('//tbody[@id="index:srednjiKursLista:tbody_element"]/tr') as $node) {
    $rowData = array();
    foreach ($xpath->query('td', $node) as $cell) {
        $rowData[] = $cell->nodeValue;
    }

    $data[] = array_combine($headerNames, $rowData);
}

print_r($data);

Output:

Array
(
    [0] => Array
        (
            [?IFRA VALUTE] => 978
            [NAZIV ZEMLJE] => EMU
            [OZNAKA VALUTE] => EUR
            [VA?I ZA] => 1
            [SREDNJI KURS] => 104,2182
        )

    ...
)

Example usage:

foreach ($data as $entry) {
    printf(
        '%s %s' . PHP_EOL,
        $entry['OZNAKA VALUTE'],
        $entry['SREDNJI KURS']
    );
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...