HTMLテーブルの変換 into JSON、CSV、およびExcelスプレッドシートを使用 GrabzItのRuby API ここに示されている例に従ってください。 ただし、開始する前に、 url_to_table, html_to_table or file_to_table メソッド save or save_to メソッドを呼び出してテーブルをキャプチャする必要があります。 このサービスが適切かどうかをすばやく確認したい場合は、 HTMLテーブルのキャプチャのライブデモ URLから。
以下の例は、指定されたWebページの最初のHTMLテーブルを変換します intoa CSVドキュメント。
grabzItClient.url_to_table("https://www.tesla.com") # Then call the save or save_to method
grabzItClient.html_to_table("<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 save_to method
grabzItClient.file_to_table("tables.html") # Then call the save or save_to method
Webページの最初のテーブルを自動的に変換したくない場合は、 tableNumberToInclude
方法。 たとえば、2を指定すると、Webページにある2番目のテーブルが変換されます。
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.tableNumberToInclude = 2 grabzItClient.url_to_table("https://www.tesla.com", options) # Then call the save or save_to method grabzItClient.save_to("result.csv"
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.tableNumberToInclude = 2 grabzItClient.html_to_table("<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 save_to method grabzItClient.save_to("result.csv")
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.tableNumberToInclude = 2 grabzItClient.file_to_table("tables.html", options) # Then call the save or save_to method grabzItClient.save_to("result.csv")
また指定することができます targetElement
指定された要素ID内のテーブルのみが変換されることを保証するメソッド。
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.targetElement = "stocks_table" grabzItClient.url_to_table("https://www.tesla.com", options) # Then call the save or save_to method grabzItClient.save_to("result.csv")
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.targetElement = "stocks_table" grabzItClient.html_to_table("<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 save_to method grabzItClient.save_to("result.csv")
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.targetElement = "stocks_table" grabzItClient.file_to_table("tables.html", options) # Then call the save or save_to method grabzItClient.save_to("result.csv")
XLSX形式を使用する場合、trueを渡すことにより、Webページ上のすべてのテーブルをキャプチャできます。 includeAllTables
方法。 これにより、各テーブルがスプレッドシートブック内の新しいシートに配置されます。
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.format = "xlsx" options.includeAllTables = true grabzItClient.url_to_table("https://www.tesla.com", options) # Then call the save or save_to method grabzItClient.save_to("result.xlsx")
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.format = "xlsx" options.includeAllTables = true grabzItClient.html_to_table("<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 save_to method grabzItClient.save_to("result.xlsx")
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.format = "xlsx" options.includeAllTables = true grabzItClient.file_to_table("tables.html", options) # Then call the save or save_to method grabzItClient.save_to("result.xlsx")
GrabzItを使用すると、RubyはHTMLテーブルを簡単に変換できます intoこれを指定するJSON json
formatパラメーターで。 以下の例では、データが読み込まれます 同期して 使用して、 save_to
メソッドとして、JSONを取得する string。 これは、次のようなライブラリによって解析できます json ジェム.
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.format = "json" options.tableNumberToInclude = 1 grabzItClient.url_to_table("https://www.tesla.com", options) json = grabzItClient.save_to()
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.format = "json" options.tableNumberToInclude = 1 grabzItClient.html_to_table("<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 = grabzItClient.save_to()
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.format = "json" options.tableNumberToInclude = 1 grabzItClient.file_to_table("tables.html", options) json = grabzItClient.save_to()
にカスタム識別子を渡すことができます テーブル メソッドを以下に示すように、この値はGrabzIt Rubyハンドラーに返されます。 たとえば、このカスタム識別子はデータベース識別子であり、スクリーンショットを特定のデータベースレコードに関連付けることができます。
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.customId = "123456" grabzItClient.url_to_table("https://www.tesla.com", options) # Then call the save method grabzItClient.save("http://www.example.com/handler/index")
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.customId = "123456" grabzItClient.html_to_table("<html><body><h1>Hello World!</h1></body></html>", options) # Then call the save method grabzItClient.save("http://www.example.com/handler/index")
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.customId = "123456" grabzItClient.file_to_table("example.html", options) # Then call the save method grabzItClient.save("http://www.example.com/handler/index")