【发布时间】:2020-06-17 03:10:42
【问题描述】:
首先我知道有很多关于这个的话题,但我没有在其中任何一个中找到解决方案。 我的问题如下,我想用 php 使用“file_get_contents”2 数据从 div 中具有相同名称的站点中提取。 我需要提取数据,然后用 PHP 为每个数据分配一个特定的变量。 无论如何,这里是不返回任何内容的代码 sn-p。
$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”。我需要一方面提取“购买”数据,另一方面提取“销售”数据
【问题讨论】:
-
正则表达式中的一个空格不会匹配多个空格和换行符。更简单的选项,顺便说一句:DOM 遍历(有时,虽然不在此处),或 strip_tags() 并仅匹配文本 sn-ps。
标签: php web-scraping preg-match file-get-contents