Web Scrapping - Extract currency exchange rate data from Nepal Rastra Bank (NRB) website to your website

Web scraping (web harvesting or web data extraction) is a computer software technique of extracting information from websites.

 

http://<your-web-site>/web-scrapping.php?YY=2014&MM=03&DD=09

<?php
$YYYY = isset($_GET['YY']) ? $_GET['YY'] : date('Y');
$MM = isset($_GET['MM']) ? $_GET['MM'] : date('m');
$DD = isset($_GET['DD']) ? $_GET['DD'] : date('d');

include('simplehtmldom/simple_html_dom.php');
$html = new simple_html_dom();

$html->load_file("http://nrb.org.np/fxmexchangerate1.php?YY=$YYYY&MM=$MM&DD=$DD");

$table = $html->find('table[width="386"]');

$i = 0;
$data = array();
foreach ($table[0]->find('tr') as $tr) {
  $i++;
  if ($i == 1 || $i == 2) continue;
  $td = $tr->find('td');
  array_push($data, array(
    'currency' => preg_replace('/\s+/', ' ', trim($td[0]->plaintext)),
    'unit' => trim($td[1]->plaintext),
    'buing_per_rs' => trim($td[2]->plaintext),
    'selling_per_rs' => trim($td[3]->plaintext),
  ));
}
$html->clear();
unset($html);

if (empty($data)) exit('No data !!!');

$data contains an array of exchange rate data from NRB.

 

Print exchange rate data in Array format.

print '<pre>';
print_r($data);
print '</pre>';

Array Output:

Array ( [0] => Array ( [currency] => U.S. dollar [unit] => 1 [buing_per_rs] => 97.44 [selling_per_rs] => 98.04 ) [1] => Array ( [currency] => European euro [unit] => 1 [buing_per_rs] => 135.46 [selling_per_rs] => 136.29 ) [2] => Array ( [currency] => UK pound sterling [unit] => 1 [buing_per_rs] => 163.33 [selling_per_rs] => 164.33 ) [3] => Array ( [currency] => Swiss franc [unit] => 1 [buing_per_rs] => 111.11 [selling_per_rs] => 111.80 ) [4] => Array ( [currency] => Australian dollar [unit] => 1 [buing_per_rs] => 88.91 [selling_per_rs] => 89.45 ) [5] => Array ( [currency] => Canadian dollar [unit] => 1 [buing_per_rs] => 88.64 [selling_per_rs] => 89.18 ) [6] => Array ( [currency] => Singapore dollar [unit] => 1 [buing_per_rs] => 77.11 [selling_per_rs] => 77.58 ) [7] => Array ( [currency] => Japanese yen [unit] => 10 [buing_per_rs] => 9.47 [selling_per_rs] => 9.52 ) [8] => Array ( [currency] => Chinese renminbi (Yuan) [unit] => 1 [buing_per_rs] => 15.91 [selling_per_rs] => 16.00 ) [9] => Array ( [currency] => Saudi Arabian riyal [unit] => 1 [buing_per_rs] => 25.98 [selling_per_rs] => 26.14 ) [10] => Array ( [currency] => Qatari riyal [unit] => 1 [buing_per_rs] => 26.76 [selling_per_rs] => 26.92 ) [11] => Array ( [currency] => Thai baht [unit] => 1 [buing_per_rs] => 3.02 [selling_per_rs] => 3.04 ) [12] => Array ( [currency] => UAE Dirham [unit] => 1 [buing_per_rs] => 26.53 [selling_per_rs] => 26.69 ) [13] => Array ( [currency] => Malaysian ringgit [unit] => 1 [buing_per_rs] => 29.91 [selling_per_rs] => 30.09 ) [14] => Array ( [currency] => South Korean Won [unit] => 100 [buing_per_rs] => 9.18 [selling_per_rs] => 9.24 ) ) 

 

Convert Array data into JSON data and print exchange rate data in JSON format.

$json = json_encode($data);
print_r($json);

JSON Output:

[{"currency":"U.S. dollar","unit":"1","buing_per_rs":"97.44","selling_per_rs":"98.04"},{"currency":"European euro","unit":"1","buing_per_rs":"135.46","selling_per_rs":"136.29"},{"currency":"UK pound sterling","unit":"1","buing_per_rs":"163.33","selling_per_rs":"164.33"},{"currency":"Swiss franc","unit":"1","buing_per_rs":"111.11","selling_per_rs":"111.80"},{"currency":"Australian dollar","unit":"1","buing_per_rs":"88.91","selling_per_rs":"89.45"},{"currency":"Canadian dollar","unit":"1","buing_per_rs":"88.64","selling_per_rs":"89.18"},{"currency":"Singapore dollar","unit":"1","buing_per_rs":"77.11","selling_per_rs":"77.58"},{"currency":"Japanese yen","unit":"10","buing_per_rs":"9.47","selling_per_rs":"9.52"},{"currency":"Chinese renminbi (Yuan)","unit":"1","buing_per_rs":"15.91","selling_per_rs":"16.00"},{"currency":"Saudi Arabian riyal","unit":"1","buing_per_rs":"25.98","selling_per_rs":"26.14"},{"currency":"Qatari riyal","unit":"1","buing_per_rs":"26.76","selling_per_rs":"26.92"},{"currency":"Thai baht","unit":"1","buing_per_rs":"3.02","selling_per_rs":"3.04"},{"currency":"UAE Dirham","unit":"1","buing_per_rs":"26.53","selling_per_rs":"26.69"},{"currency":"Malaysian ringgit","unit":"1","buing_per_rs":"29.91","selling_per_rs":"30.09"},{"currency":"South Korean Won","unit":"100","buing_per_rs":"9.18","selling_per_rs":"9.24"}]

 

Create JSON file of exchange rate data.

$fp = fopen("nrb-exchange-rate-$YYYY-$MM-$DD.json", 'w');
fwrite($fp, $json);
fclose($fp);

 

Important links: