Schwerwiegender Fehler: Nicht abgefangener Fehler: Aufruf der Mitgliedsfunktion find() auf bool in C. Stack-Trace: #0 {main} wird in C in Zeile 16 ausgelöst
P粉878510551
P粉878510551 2023-12-28 23:57:42
0
1
371

Ich erhalte diese Fehlermeldung oben und mein Code ist unten. Stimmt etwas mit meinem Code nicht? Jede Hilfe wird geschätzt. Ich bin mir nicht sicher, ob es mit der Software zusammenhängt, die ich zum Ausführen verwende, XAMPP Apache. Ich versuche derzeit, Informationen von einer Webseite abzurufen, die Produktcodes in Excel enthält. Ich habe es geschafft, über 900 Produktionscodes auszuführen, aber plötzlich bekam ich die obige Fehlermeldung.

<?php
// example of how to use basic selector to retrieve HTML contents
include('simple_html_dom.php');

$file = fopen("Book1.csv","r");
$file2 = fopen("test.csv","w");
$links = [];

while (($result = fgetcsv($file)) !== false)
{
    $link = "https://mall/Product/".$result[0];
    $links[] = $link;

    $row_data = [];
    $page = file_get_html($link);
    $product_details = $page->find('.ProductDetailsTable tr'); //line 16
    if(count($product_details)==0) {
        $row_data[] = $result[0];
        $row_data[] = 'not found'; 
        fputcsv($file2, $row_data);
        continue;
    }

    //second method
    $article_number = '';
    $product_description = '';
    $product_family = '';
    $product_lifecycle = '';
    $plm_date = '';
    $notes = '';
    $EAN = '';
    $UPC = '';
    $country_of_origin = '';

    foreach($product_details as $table_row) {    
        if(count($table_row->find('td'))==1){
            //ignore
        } elseif(count($table_row->find('td'))==2) {
            $key = $table_row->find('td')[0]->plaintext;
            $value = $table_row->find('td')[1]->plaintext;
            if($key=="EAN") {
                $EAN = $value;
            }
            elseif($key=='Article Number (Market Facing Number)') {
                $article_number = $value;
            } elseif ($key=='Product Description') {
                $product_description = $value;
            } elseif ($key=='Product family') {
                $product_family = $value;
            }elseif ($key=='Product Lifecycle (PLM)') {
                $product_lifecycle = $value;
            }elseif ($key=='PLM Effective Date') {
                $plm_date = $value;
            }elseif ($key=='Notes') {
                $notes = $value;
            }elseif ($key=='UPC') {
                $UPC = $value;
            }elseif ($key=='Country of origin') {
                $country_of_origin = $value;
            }    
        } 
    }
    $row_data[] = trim($article_number);
    $row_data[] = trim($product_description);
    $row_data[] = trim($product_family);
    $row_data[] = trim($product_lifecycle);
    $row_data[] = trim($plm_date);
    $row_data[] = trim($notes);
    $row_data[] = trim($EAN);
    $row_data[] = trim($UPC);
    $row_data[] = trim($country_of_origin);
    fputcsv($file2, $row_data);
}

fclose($file);
fclose($file2);
echo 'done';

?>

P粉878510551
P粉878510551

Antworte allen(1)
P粉237647645

发生这种情况是因为 file_get_html() 返回一个布尔值(可能是 false 由于一些错误,例如无效的 url)。

您的代码没有检查失败。

我建议您添加以下内容:

if (!$page) {
  // Do some error handling, logging
  continue; // skip to next iteration on your loop.
}
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!