2017-10-05 6 views
0

を見つからない場合はスキップ私は別のウェブサイトからHTMLをダウンロードするために使用していますのtry/catchループを作成しようとしています:データが存在する場合がつがつ食うHTTP - 例外が存在するか、ページ

foreach($intldes as $id) { 
    $html = HtmlDomParser::file_get_html('https://nssdc.gsfc.nasa.gov/nmc/spacecraftDisplay.do?id='.$id); 
    foreach($html->find('#rightcontent') as $id); 
    foreach($html->find('.urone p') as $element); 
    foreach($html->find('.urtwo') as $launchdata); 
} 

、それは、その結果以下のHTML:

<p><strong>NSSDCA/COSPAR ID:</strong> 2009-038F</p> 
 
<p>ANDE 2, the Atmospheric Neutral Density Experiment 2, is a pair of microsatellites (Castor and Pollux) launched from Cape Canaveral on STS 127 on 15 July 2009 at 22:03 UT and deployed from the payload bay of the shuttle on 30 July 2009 at 17:22 UT.</p> 
 
<p><strong>Launch Date:</strong> 2009-07-15<br/><strong>Launch Vehicle:</strong> Shuttle<br/><strong>Launch Site:</strong> Cape Canaveral, United States<br/></p>

データが存在しない場合、私はDOMことを意味Undefined variable: elementエラーを取得パーサーは、表示したいHTMLを見つけることができませんでした。

私は必要なHTMLを持たないWebページをスキップするか、NULLの変数を返します。

基本的には、HTMLが必要な場合や変数$elementが存在しない場合は、GuzzleがそのWebページをスキップして読み込まないようにします。

EDIT:

私のフル機能:

public function tester() { 
    $intldes = DB::table('examples')->pluck('id'); 
    foreach ($intldes as $query) { 
     $html = HtmlDomParser::file_get_html('https://example.com?id='.$query); 
     $elements = $html->find('.urone p', 0); 
    if (is_array($elements)) { 
     foreach($html->find('#rightcontent') as $rawid); 
     foreach($html->find('.urone p') as $rawdescription); 
     foreach($html->find('.urtwo') as $launchdata); 

     //-- Data Parser --// 
     //Intldes 
     $intldesgetter = strip_tags($rawid->first_child()->next_sibling()->next_sibling()); //Get Element and Remove Tags 
     $intldesformat = substr($intldesgetter, ($pos = strpos($intldesgetter, ':')) !== false ? $pos + 3 : 0); //Remove Title 
     $dbintldes = ltrim($intldesformat); //Remove Blank-space 

     //Description 
     $description = strip_tags($rawdescription); 
     $dbdescription = ltrim($description); 

     //Launch Data 
     $launchdate = $launchdata->first_child()->next_sibling()->next_sibling()->next_sibling(); 
     $explode = explode("<br/>", $launchdate); 
     $newArray = array_map(function($v){ 
      return trim(strip_tags($v)); 
     }, $explode); 
     $dblaunchdate = substr($newArray[0], ($pos = strpos($newArray[0], ':')) !== false ? $pos + 3 : 0); 
     $dblaunchvehicle = substr($newArray[1], ($pos = strpos($newArray[1], ':')) !== false ? $pos + 3 : 0); 
     $dblaunchsite = substr($newArray[2], ($pos = strpos($newArray[2], ':')) !== false ? $pos + 3 : 0); 

     //Data Saver 
     DB::table('descriptions')->insert(
      ['intldes' => $dbintldes, 'description' => strip_tags($dbdescription), 'launch_date' => $dblaunchdate, 'launch_vehicle' => $dblaunchvehicle, 'launch_site' => $dblaunchsite] 
     ); 
     echo "Success"; 
     } else { 
      echo "$query does not exist"; 
      continue; 
     }; 
    } 
} 

答えて

0

私はあなたのコードでは、ここでエラーを取得していると思う:

foreach($html->find('.urone p') as $element); 

私の経験から、私はあなたにお勧めします反復する前に、HTMLタグの可用性を最初に確認する必要がありますforeachループ。

問題を回避するには、is_object()またはis_array()のいずれかを使用できます。 1つの要素を検索すると、オブジェクトが返されます。要素のセットを検索すると、オブジェクトの配列が返されます。

あなたは要素のセットを探していると、あなたは

$elements = $html->find('.urone p'); 
if (is_array($elements)) { 
    //continue 
} 
+0

はちょうどそれを試してみました使用することができます。 '$ elements'が存在する場合に機能しますが、存在しない場合は' Undefined variable'というエラーが発生します。 – spacetravel

+0

'$ element = $ html-> find( '。urone p'、0)'を試して、 '$ element'がヌルであるかどうかを調べることができます。 –

+0

まだ動作しませんでした - 違う。私が抱えている問題の背景について教えてください:データベースから列を引きます。次に、列のデータを使って 'foreach'というURLを作成します。つまり、URLは/ 1、/ 2、/ 3など(私が引っ張った私の列のもの)です。現在実行しようとしているのは、たとえば/ 6というURLが存在しない場合はスキップすることです。これは、あなたの質問で提供したHTMLの 'if'ステートメントを使って行います。 – spacetravel

関連する問題