【问题标题】:Displaying data (retrieved from a website) in a table with PHP使用 PHP 在表格中显示数据(从网站检索)
【发布时间】:2017-08-14 22:57:07
【问题描述】:

我想用 PHP 从this site 中检索数据并显示出来。

我使用 PHP 中的 pregmatch 函数在 3 个不同的表中检索了所需的值(从文章中的文章名称、价格以及下面的其他值)。剩下的就是将它们显示在一个二维的表格中。

表格的第一行应该有文章名称和价格。其余的行应包含标题及其值。

这是我当前的 PHP 代码:

<?php

$debut="https://www.agriconomie.com";
$txt = file_get_contents('https://www.agriconomie.com/pieces-agricoles/tracteur/attelage---relevage/pc2902');    /*ici c'est pour Lire la page html*/

$results = array();
// $test = preg_match_all('#<a href="(.*?)">#', $txt, $names_array);

$test = preg_match_all('#<a href="(.+)" class="(.+)" title="(.+)"#', $txt, $names_array);

/*recupéré les liens du site  en particuliers le text qui se situe entre griffe "" du href*/

for($i = 0; $i < count($names_array[1]); $i++) 
{
    $j=$i;

    $debut="https://www.agriconomie.com".$names_array[1][$i]; 

    $adresse =$debut;
    /* echo $adresse ; ?>    <br /> <?php */

    $page = file_get_contents ($adresse);

    /* preg_match_all ('#<h3 class="product-name">(.+)</h3>#', $page, $names_array5); */                       
    preg_match_all ('#(<dd>(.+)</dd>)#', $page, $names_array2); 
    preg_match_all ('#<span><i class="icon-chevron-right"></i>(.*?)</span>#', $page, $names_array3); 
    preg_match_all ('#<p class="price" itemprop="price" content="(.*?)">#', $page, $names_array4);

    echo "<center>";

    echo "<table class='table table-bordered table-striped table-condensed'>";

    /*
    for($j = 0; $j < count($names_array5[1]); $j++)  
    {
        $NOM   =  $names_array5[1][$j]; 

        echo  'Nom ='.$NOM ; 
    }   
    */ 

    for($j = 0; $j < count($names_array4[1]); $j++)  
    {
        $price   =  $names_array4[1][$j]; 
        echo     'Prix ='.$price.'$' ; 
    } 


    for($i = 0; $i < count($names_array3[1]); $i++) 
    {
        for($j= 0; $j < count($names_array2[1]); $j++){
            $descriptif   =  $names_array2[1][$i];   
        }

        $intitule   =  $names_array3[1][$i]; 
        echo "<tr><td>".$intitule." </td>  <td> ".$descriptif." </td> </tr> ";
    } 
}

echo "</table>";
echo "</center>";

?>

【问题讨论】:

  • 请在发布您的问题之前格式化您的代码。
  • 如何格式化我的代码?这是我的源代码!请帮助我。
  • 您的代码已被@Qirel 格式化和编辑
  • 谢谢!我希望有人可以帮助我。
  • 您已经描述了您需要实现的目标,但您没有告诉我们您在工作时遇到的具体问题。

标签: php regex preg-match-all


【解决方案1】:

我发现了很多要纠正/整理的东西,所以我几乎完全重写了。

$debut="https://www.agriconomie.com";
$txt = file_get_contents('https://www.agriconomie.com/pieces-agricoles/tracteur/attelage---relevage/pc2902');

if(!preg_match_all('#<a href="([^"]*?)".*?title="([^"]*?)"#',$txt,$desarticles)){exit("Failure @ desarticles");}
foreach($desarticles[1] as $i=>$url_ext){
    $page=file_get_contents("https://www.agriconomie.com{$url_ext}");  // https://www.agriconomie.com/clips-ordinaire-de-9x45-le-cent/p207990

    if(!preg_match_all('#<p class="price" itemprop="price" content="(.*?)">#',$page,$desprix)){exit("Failure @ desprix ($i)");}
    if(!preg_match_all('#<i class="icon-chevron-right"><\/i>(.*?)<\/span>.*?<dd>(.+)<\/dd>#s',$page,$information)){exit("Failure @ information ($i)");}

    echo "<center>";
        echo "<table class='table table-bordered table-striped table-condensed'>";
            echo "<tr>";
                echo "<td>{$desarticles[2][$i]}</td>";  // borrow $i from iteration of $desarticles[1]
                echo "<td>Prix ={$desprix[1][0]}$</td>";  // Price (only one per loop)
            echo "</tr>";
            foreach($information[1] as $k=>$info){
                echo "<tr>";
                    echo "<td>{$info}</td>";
                    echo "<td>{$information[2][$k]}</td>";  // borrow $k from iteration of $information[1]
                echo "</tr>";
            }
        echo "</table>";
    echo "</center>";
}

一些更好的点:

  • 我加快了 $desarticles 上的正则表达式并省略了中间捕获组。
  • 我删除了一些不必要的变量。
  • 我用 foreach 循环替换了 iterate/count for 循环(以避免计数条件)。
  • 我将两个 preg_match_all 行合并为一个名为 $information 的行。
  • 我在 $information 的正则表达式中转义了结束标签。
  • 我按照要求创建了一个干净、基本的两列表结构。

【讨论】:

  • 我现在年纪大了,也聪明多了(这个答案是从我第一次开始发布答案的时候开始的——当时我显然并不太担心编码标准和最佳实践)。我不再推荐使用 preg_ 函数解析 html。合适的 DOM 解析器是合适的工具。
猜你喜欢
  • 1970-01-01
  • 2021-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多