HTMLテーブルを変換する方法の次の例を使用します into JSON、CSV、およびExcelスプレッドシートを使用 GrabzItのPerl 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を tableNumberToInclude
方法。
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->tableNumberToInclude(2); $grabzIt->URLToTable("https://www.tesla.com", $options); # Then call the Save or SaveTo method $grabzIt->SaveTo("result.csv");
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->tableNumberToInclude(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 = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->tableNumberToInclude(2); $grabzIt->FileToTable("tables.html", $options); # Then call the Save or SaveTo method $grabzIt->SaveTo("result.csv");
また指定することができます targetElement
指定された要素ID内のテーブルのみが変換されることを保証するメソッド。
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->targetElement("stocks_table"); $grabzIt->URLToTable("https://www.tesla.com", $options); # Then call the Save or SaveTo method $grabzIt->SaveTo("result.csv");
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->targetElement("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 = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->targetElement("stocks_table"); $grabzIt->FileToTable("tables.html", $options); # Then call the Save or SaveTo method $grabzIt->SaveTo("result.csv");
または、trueを渡すことにより、Webページ上のすべてのテーブルをキャプチャできます。 includeAllTables
ただし、これはXLSX形式でのみ機能します。 このオプションは、生成されたスプレッドシートブック内の新しいシートに各テーブルを配置します。
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->format('xlsx'); $options->includeAllTables(1); $grabzIt->URLToTable("https://www.tesla.com", $options); # Then call the Save or SaveTo method $grabzIt->SaveTo("result.xlsx");
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->format('xlsx'); $options->includeAllTables(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); # Then call the Save or SaveTo method $grabzIt->SaveTo("result.xlsx");
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->format('xlsx'); $options->includeAllTables(1); $grabzIt->FileToTable("tables.html", $options); # Then call the Save or SaveTo method $grabzIt->SaveTo("result.xlsx");
PerlとGrabzItを使用すると、見つかったHTMLテーブルを変換できます into JSON。 開始するには、 json
formatパラメーターで。 この例では、 同期呼び出し 使用して、 SaveTo
JSONを返すメソッド string、これは次のようなライブラリで解析できます JSON :: Parse.
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->format("json"); $options->tableNumberToInclude(1); $grabzIt->URLToTable("https://www.tesla.com", $options); $json = $grabzIt->SaveTo();
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->format("json"); $options->tableNumberToInclude(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();
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->format("json"); $options->tableNumberToInclude(1); $grabzIt->FileToTable("tables.html", $options); $json = $grabzIt->SaveTo();
にカスタム識別子を渡すことができます テーブル メソッドを以下に示すように、この値はGrabzIt Perlハンドラーに返されます。 たとえば、このカスタム識別子はデータベース識別子であり、スクリーンショットを特定のデータベースレコードに関連付けることができます。
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->customId("123456"); $grabzIt->URLToTable("https://www.tesla.com", $options); # Then call the Save method $grabzIt->Save("http://www.example.com/handler.pl");
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->customId("123456"); $grabzIt->HTMLToTable("<html><body><h1>Hello World!</h1></body></html>", $options); # Then call the Save method $grabzIt->Save("http://www.example.com/handler.pl");
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->customId("123456"); $grabzIt->FileToTable("example.html", $options); # Then call the Save method $grabzIt->Save("http://www.example.com/handler.pl");