【问题标题】:Export data to CSV file with php and simple html dom使用 php 和简单的 html dom 将数据导出到 CSV 文件
【发布时间】:2017-09-14 12:27:27
【问题描述】:

如何将抓取的数据导出到 .csv 文件?我正在使用 php simple html dom 来解析数据。代码如下:

foreach ($linkoviStranica as $stranica)
{
    $podaciStranice = "http://someurl/$stranica";

    $data = file_get_html($podaciStranice);


    $name = $data->find('div[class="price-card-name-header-name"]');
    $onlinePrice = $data->find('div[class="price-box online"]');
    $diffPrice = $data->find('div[class="price-box paper"]');

     echo "<strong>".$name[0]->innertext."<strong>"."<br>";
      if (!empty($onlinePrice[0]->innertext))
      { 
        echo $onlinePrice[0]->innertext."<br>";
      }
     if (!empty($diffPrice[0]->innertext))
     {  
         echo $diffPrice[0]->innertext."<br>";

         echo "---------------------"."<br>";

     }
 }

我想将 $name, $onlinePrice, $diffPrice 导出到带有以下格式标题的 csv 文件:

name    onlinePrice diffPrice
example    10          44
xxxx       412        461
zzzzz     1414         41

你能帮帮我吗?谢谢!

【问题讨论】:

    标签: php html csv dom export


    【解决方案1】:

    类似这样的:

    // Define a array to hold all the data
    $data = [];
    
    // OR use this line if you want the headers with names on the first line of the file
    // $data = [['name', 'onlinePrice', 'diffPrice']];
    
    // Loop the raw data
    foreach ($linkoviStranica as $stranica) {
        $podaciStranice = "http://someurl/$stranica";
        $data = file_get_html($podaciStranice);
    
        $name = $data->find('div[class="price-card-name-header-name"]');
        $onlinePrice = $data->find('div[class="price-box online"]');
        $diffPrice = $data->find('div[class="price-box paper"]');
    
        // Add current row to out array
        $data[] = [
            $name[0]->innertext,
            $onlinePrice[0]->innertext,
            $diffPrice[0]->innertext
        ];
    }
    
    // Open a new file. Replace the file name with the name you'd like
    $fp = fopen('file.csv', 'w');
    
    // Loop each row in the data array
    foreach ($data as $fields) {
        // This method converts a row to a CSV line (http://php.net/manual/en/function.fputcsv.php)
        fputcsv($fp, $fields);
    }
    
    // Close the file handler
    fclose($fp);
    

    【讨论】:

    • tnx 这就是我一直在寻找的,稍作改动后,我添加了标题: fputcsv($fp, array('name', 'online price', 'diff price') );因为 data['name', 'online price', 'diff price'] 给了我一个错误。
    • @dilesko 查看我的代码 sn-p 中的第四行。它添加了这个标题。如果它解决了您的问题,请接受我的回答,以便人们以后更容易找到它。
    【解决方案2】:
    $header ="SNo , Order Date , Order Number , Compaign , Amount , Order Status , Used Date , Cancel Reason , Order Commision , Payment Date , Payment Status \n";
                $header1 =$header ;
    $result='';
    foreach ($data as $fields) {
      $result= 'echo your data with coma seprated '."\n";
    }
    $all=$header.$result;
    $name="report.csv";
    header("Content-type: application/csv");
    header("Content-Disposition: attachment; filename=".$name);
    header("Pragma: no-cache");
    header("Expires: 0");
    print "$header1 $result";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-25
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2013-04-16
      • 1970-01-01
      相关资源
      最近更新 更多