Web抓取PHP特定div

问题描述 投票:0回答:1

首先,我知道有很多与此相关的主题,但是我都没有找到解决方案。我的问题是以下,我想使用“ file_get_contents” 2数据从div中具有相同名称的站点中提取php。我需要提取数据,然后使用PHP为每个变量分配一个特定的变量。无论如何,这是不返回任何内容的代码段。

$htmlOficial = file_get_contents('https://www.dolarhoy.com/cotizaciondolaroficial');
preg_match('/<tr><td><a href="#">Banco Nacion</a></td><td class="number">(.*)</td>/', 
$htmlOficial, $ventaOficial);
preg_match('/<tr><td><a href="#">Banco Nacion</a></td><td class="number"></td> <td class="number">(.*)</td>
            </tr>/', 
$htmlOficial, $compraOficial);
$ventaOficial = $ventaOficial[1];
$compraOficial = $compraOficial[1];

该站点为https://www.dolarhoy.com/cotizaciondolaroficial,在“实体”框中显示“ Banco Nacion”。我需要一方面提取“购买”,另一方面提取“销售”的数据

php web-scraping preg-match file-get-contents
1个回答
0
投票
[...] $text_chunks = explode('Banco Nacion', $htmlOficial); // We ignore $text_chunks[0] since is the text until the first "Banco Nacion" string $ventaOficial = strbtw('class="number">', '</td>', $text_chunks[1]); $compraOficial = strbtw('class="number">', '</td>', $text_chunk[2]); function strbtw($text, $str1, $str2="", $trim=true) { $len = strlen($str1); $pos_str1 = strpos($text, $str1); if ($pos_str1 === false) return ""; $pos_str1+=$len; if (empty($str2)) { // try to search up to the end of line $pos_str2 = strpos($text, "\n", $pos_str1); if ($pos_str2 === false) $pos_str2 = strpos($text, "\r\n", $pos_str1); } else $pos_str2 = strpos($text, $str2, $pos_str1); if ($pos_str2 !== false) { if ($pos_str2-$pos_str1 === 0) $rez = substr($text, $pos_str1); else $rez = substr($text, $pos_str1, $pos_str2-$pos_str1); } else $rez = substr($text, $pos_str1); return ($trim) ? trim($rez) : ($rez); }

请让我知道它是否有效。

© www.soinside.com 2019 - 2024. All rights reserved.