HTMLテーブルを変換する方法は複数あります into JSON、CSV、またはExcelスプレッドシートを使用 GrabzItのPHP APIここで詳しく説明するのは、最も便利なテクニックの一部です。 ただし、開始する前に、 URLToTable, HTMLToTable or FileToTable メソッド Save or SaveTo メソッドを呼び出してテーブルをキャプチャする必要があります。 このサービスが適切かどうかをすばやく確認したい場合は、 HTMLテーブルのキャプチャのライブデモ URLから。
以下のコード例は、指定されたWebページで最初に発見されたHTMLテーブルを自動的に変換します intoa CSVドキュメント。
$grabzIt->URLToTable("https://www.tesla.com"); //Then call the Save or SaveTo method
$grabzIt->HTMLToTable("<html><body><table><tr><th>Name</th><th>Age</th></tr> <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr> </table></body></html>"); //Then call the Save or SaveTo method
$grabzIt->FileToTable("tables.html"); //Then call the Save or SaveTo method
デフォルトでは、これは、識別する最初のテーブルを変換します intテーブル。 ただし、Webページの2番目のテーブルは、2を setTableNumberToInclude
方法。
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = new \GrabzIt\GrabzItTableOptions(); $options->setTableNumberToInclude(2); $grabzIt->URLToTable("https://www.tesla.com", $options); //Then call the Save or SaveTo method $grabzIt->SaveTo("result.csv");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = new \GrabzIt\GrabzItTableOptions(); $options->setTableNumberToInclude(2); $grabzIt->HTMLToTable("<html><body><table><tr><th>Name</th><th>Age</th></tr> <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr> </table></body></html>", $options); //Then call the Save or SaveTo method $grabzIt->SaveTo("result.csv");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = new \GrabzIt\GrabzItTableOptions(); $options->setTableNumberToInclude(2); $grabzIt->FileToTable("tables.html", $options); //Then call the Save or SaveTo method $grabzIt->SaveTo("result.csv");
使用することもできます setTargetElement
指定された要素ID内のテーブルのみが変換されることを保証するメソッド。
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = new \GrabzIt\GrabzItTableOptions(); $options->setTargetElement("stocks_table"); $grabzIt->URLToTable("https://www.tesla.com", $options); //Then call the Save or SaveTo method $grabzIt->SaveTo("result.csv");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = new \GrabzIt\GrabzItTableOptions(); $options->setTargetElement("stocks_table"); $grabzIt->HTMLToTable("<html><body><table id='stocks_table'><tr><th>Name</th><th>Age</th></tr> <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr> </table></body></html>", $options); //Then call the Save or SaveTo method $grabzIt->SaveTo("result.csv");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = new \GrabzIt\GrabzItTableOptions(); $options->setTargetElement("stocks_table"); $grabzIt->FileToTable("tables.html", $options); //Then call the Save or SaveTo method $grabzIt->SaveTo("result.csv");
または、trueを渡すことにより、Webページ上のすべてのテーブルをキャプチャできます。 setIncludeAllTables
ただし、これはXLSXおよびJSON形式でのみ機能します。 このオプションは、生成されたスプレッドシートブック内の新しいシートに各テーブルを配置します。
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = new \GrabzIt\GrabzItTableOptions(); $options->setFormat('xlsx'); $options->setIncludeAllTables(true); $grabzIt->URLToTable("https://www.tesla.com", $options); //Then call the Save or SaveTo method $grabzIt->SaveTo("result.xlsx");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = new \GrabzIt\GrabzItTableOptions(); $options->setFormat('xlsx'); $options->setIncludeAllTables(true); $grabzIt->HTMLToTable("<html><body><table><tr><th>Name</th><th>Age</th></tr> <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr> </table></body></html>", $options); //Then call the Save or SaveTo method $grabzIt->SaveTo("result.xlsx");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = new \GrabzIt\GrabzItTableOptions(); $options->setFormat('xlsx'); $options->setIncludeAllTables(true); $grabzIt->FileToTable("tables.html", $options); //Then call the Save or SaveTo method $grabzIt->SaveTo("result.xlsx");
プログラムによってHTMLテーブルを読み取る必要がある場合があります。GrabzItを使用すると、オンラインHTMLテーブルを変換してPHPを使用してこれを行うことができます。 into JSON。 これを行うには、指定します json
フォーマットパラメータとして。 たとえば、次の例では、HTMLテーブルを変換しています 同期して その後、組み込みを使用して json_decode
JSONを解析するPHPメソッド string into簡単に作業できるオブジェクト。
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = new \GrabzIt\GrabzItTableOptions(); $options->setFormat("json"); $options->setTableNumberToInclude(1); $grabzIt->URLToTable("https://www.tesla.com", $options); $json = $grabzIt->SaveTo(); if ($json != null) { $tableObj = json_decode($json); }
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = new \GrabzIt\GrabzItTableOptions(); $options->setFormat("json"); $options->setTableNumberToInclude(1); $grabzIt->HTMLToTable("<html><body><table><tr><th>Name</th><th>Age</th></tr> <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr> </table></body></html>", $options); $json = $grabzIt->SaveTo(); if ($json != null) { $tableObj = json_decode($json); }
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = new \GrabzIt\GrabzItTableOptions(); $options->setFormat("json"); $options->setTableNumberToInclude(1); $grabzIt->FileToTable("tables.html", $options); $json = $grabzIt->SaveTo(); if ($json != null) { $tableObj = json_decode($json); }
にカスタム識別子を渡すことができます テーブル 次に示すメソッドを使用すると、この値はGrabzIt PHPハンドラーに返されます。 たとえば、このカスタム識別子はデータベース識別子であり、抽出されたテーブルを特定のデータベースレコードに関連付けることができます。
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = new \GrabzIt\GrabzItTableOptions(); $options->setCustomId(123456); $grabzIt->URLToTable("https://www.tesla.com", $options); //Then call the Save method $grabzIt->Save("http://www.example.com/handler.php");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = new \GrabzIt\GrabzItTableOptions(); $options->setCustomId(123456); $grabzIt->HTMLToTable("<html><body><h1>Hello World!</h1></body></html>", $options); //Then call the Save method $grabzIt->Save("http://www.example.com/handler.php");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = new \GrabzIt\GrabzItTableOptions(); $options->setCustomId(123456); $grabzIt->FileToTable("example.html", $options); //Then call the Save method $grabzIt->Save("http://www.example.com/handler.php");