Skip to content

Instantly share code, notes, and snippets.

@prionkor
Last active May 11, 2021 22:28
Show Gist options
  • Save prionkor/650b41c988c3c08ec2ab to your computer and use it in GitHub Desktop.
Save prionkor/650b41c988c3c08ec2ab to your computer and use it in GitHub Desktop.
Dhaka stock exchange (http://dsebd.org) company page information scrapper.
<?php
namespace Numstock\Models;
use Sunra\PhpSimple\HtmlDomParser;
class Dse_Company_Data{
private $endpoint = 'http://dsebd.org/displayCompany.php';
protected $parser, $company = false;
public function __construct( HtmlDomParser $parser ) {
$this->parser = $parser;
}
public function get($company = false) {
if(!$company)
return false;
$company = strtoupper($company);
$this->company = $company;
$remote_url = $this->endpoint . "?name=" . $company;
$response = wp_remote_get( $remote_url );
$info = new \stdClass();
if ( is_wp_error($response) )
return $response;
$html = wp_remote_retrieve_body( $response );
$dom = $this->parser;
$dom = $dom::str_get_html($html);
$table = $dom->find('body', 0)->children(2)->children(0)->children(0)->children(1)->children(1)->children(1)->children(0)->children(0)->children(2)->children(0); // td
$info->market_cap = $table->children(0)->find('table[bgcolor=#C0C0C0]', 1)->find('tr', 9)->find('font', 1)->plaintext;
// basic info
$basic_info_table = $table->children(0)->find('table', 0)->next_sibling()->find('table', 0);
$info->autorized_capital = $basic_info_table->find('tr', 2)->find('font', 1)->plaintext;
$info->paid_up_capital = $basic_info_table->find('tr', 3)->find('font', 1)->plaintext;
$info->face_value = $basic_info_table->find('tr', 5)->find('font', 1)->plaintext;
$info->market_lot = $basic_info_table->find('tr', 5)->find('font', 3)->plaintext;
$info->total_securities = $basic_info_table->find('tr', 7)->find('font', 1)->plaintext;
$info->business_segment = $basic_info_table->find('tr', 7)->find('font', 3)->plaintext;
// other info
$other_info_table = $table->children(0)->find('table[bgcolor=#C0C0C0]', 7)->find('table', 0);
$info->listing_year = $other_info_table->find('tr font', 1)->outertext;
// percentage
$share_percentage = new \stdClass();
$share_percentage->sponsor_director = (float) $this->strip_chars($other_info_table->find('table font', 0)->plaintext);
$share_percentage->govt = (float) $this->strip_chars($other_info_table->find('table font', 1)->plaintext);
$share_percentage->institute = (float) $this->strip_chars($other_info_table->find('table font', 2)->plaintext);
$share_percentage->foreign = (float) $this->strip_chars($other_info_table->find('table font', 3)->plaintext);
$share_percentage->public = (float) $this->strip_chars($other_info_table->find('table font', 4)->plaintext);
$info->share_percentage = $share_percentage;
return $info;
}
protected function strip_chars($str){
return preg_replace("/[^0-9\.]/", "", $str);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment