public function getDivsFromCode($code){ // - , . include_once './phpQuery.php'; if ( $curl = curl_init () ) // { curl_setopt ($curl, CURLOPT_URL, 'http://stocks.investfunds.ru/stocks/'.$code.'/dividend/');// curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($curl, CURLOPT_POST, true); curl_setopt ($curl, CURLOPT_HEADER, 0); $result = curl_exec ($curl);// curl_close ($curl);// } $html = phpQuery::newDocumentHTML($result); phpQuery::selectDocument($html); $a = [1,3,5]; // (, ) $emitent_string = htmlentities(pq('h1')->text(),ENT_COMPAT,'UTF-8'); if ($emitent_string === ""){ $divs['emitent'] = ' '; return $divs; } $re = [' , ', ' , ',' ',' ']; $emi = str_replace($re,'',$emitent_string); $divs['emitent'] = $emi; $divs['code'] = $code; /* , */ $i = pq('[cellpadding="0"] tr td:nth-child(1)'); $l = substr_count($i,'td style'); /*, */ if ($l === 0){ $divs['data']= 'No divs'; return $divs; } /****** css************************/ for ($i =3;$i<$l+3;$i++){ foreach ($a as $j){ switch ($j) { case 1: $divs['data'][$i-2]['year'] = str_replace(' ','',htmlentities(pq("[cellpadding='0'] tr:nth-child($i) td:nth-child($j)")->text(),ENT_COMPAT,'UTF-8')); break; case 3: $divs['data'][$i-2]['date'] = str_replace(' ','',htmlentities(pq("[cellpadding='0'] tr:nth-child($i) td:nth-child($j)")->text(),ENT_COMPAT,'UTF-8')); break; case 5: $string = htmlentities(pq("[cellpadding='0'] tr:nth-child($i) td:nth-child($j)")->text(),ENT_SUBSTITUTE,'UTF-8'); $re = '/[ \s]|[]/'; $divs['data'][$i-2]['price'] = (float)preg_replace($re,'',$string); break; default: break; } } } /* , , , */ return $divs; }
$divs = $emitent->getDivsFromCode($i); if ($divs['emitent'] != ' '){ array_push($json_div, $divs); } } file_put_contents('companies_part1.json',json_encode($json_div,JSON_UNESCAPED_UNICODE));
[{"emitent":", (RU0009071187, AVAZ)", "code":3, "data": {"1": {"year":"2007","date":"16.05.2008","price":0.29}, "2": {"year":"2006","date":"06.04.2007","price":0.1003}, "3": {"year":"2005","date":"07.04.2006","price":0.057} } }]
Minuses of the parser: at first I thought that I would have to parse about 1000 pages, because after 800 I only came across additional issues. But then, not finding one of the issuers, I decided to continue the parsing and it turned out that even after 5k pages something might happen. The solution is to rewrite the parser using multicurl for processing speed. Before that, my hands did not reach me (I began to read), but for the right one, I should have done just that. Maybe someone from the readers will help me. Again, the base with divas can be updated once every six months. That's enough if you operate with annual yields.
$name = 'GAZP'; // $year_buy = 2015; // $buy_price = 130; // $year_last_div = 2016; // $v_need = 4; //
public function getDivsFromName($name){ $file = file_get_contents('companies.json'); $array = json_decode($file,true); foreach ($array as $emitent) { if ((stristr($emitent['emitent'],$name))&&(!stristr($emitent['emitent'],$name.'-'))){ return $emitent; } } }
$emitent = new emitent(); $v_today; // ; $v_need = $v; // ; $div = $emitent->getDivsFromName($name); $sum = $emitent->getSumDivs($year_buy, $div); $last_div = $emitent->getSumDivs($year_last_div, $div,1); // c /*r = (D + (P1 - P0))/P0 * 100% * P0 - * P1 - * D - * */ $today_price = $emitent->getPrice($name, '.json'); $r = ($sum + ($today_price-$buy_price))/$buy_price*100; $v_today = $last_div/$today_price*100; $P = ($last_div/$v_need)*100; //
I note that, as in the previous article, this is only the finding of one of the factors that influence decision making.
Source: https://habr.com/ru/post/345696/
All Articles