Konwertowanie tabel HTML into Używanie arkuszy kalkulacyjnych JSON, CSV i Excel GrabzIt's Ruby API jest wschód, po prostu postępuj zgodnie z pokazanymi tutaj przykładami Jednak zanim zaczniesz, pamiętaj, że po wywołaniu url_to_table, html_to_table or file_to_table metody save or save_to należy wywołać metodę, aby przechwycić tabelę. Jeśli chcesz szybko sprawdzić, czy ta usługa jest właśnie dla Ciebie, możesz spróbować pokaz na żywo przechwytywania tabel HTML z adresu URL.
Poniższy przykład konwertuje pierwszą tabelę HTML na określonej stronie internetowej into dokument 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
Jeśli nie chcesz automatycznie konwertować pierwszej tabeli na stronie internetowej, możesz określić tableNumberToInclude
metoda. Na przykład określenie 2 przekształciłoby drugą tabelę znalezioną na stronie internetowej.
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")
Możesz także określić targetElement
metoda, która zapewni konwersję tylko tabel w ramach określonego identyfikatora elementu.
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")
Jeśli używasz formatu XLSX, możesz przechwycić wszystkie tabele na stronie, przekazując wartość true do includeAllTables
metoda. Spowoduje to umieszczenie każdej tabeli w nowym arkuszu w skoroszycie arkusza kalkulacyjnego.
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")
Dzięki GrabzIt Ruby może łatwo konwertować tabele HTML into JSON, aby to zrobić, określ json
w parametrze formatu. W poniższym przykładzie dane są odczytywane synchronicznie przy użyciu save_to
, aby uzyskać JSON jako string. Można to następnie przeanalizować za pomocą biblioteki takiej jak klejnot Jsona.
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()
Możesz przekazać niestandardowy identyfikator do stół metody pokazane poniżej, ta wartość jest następnie zwracana do twojego programu obsługi GrabzIt Ruby. Na przykład ten niestandardowy identyfikator może być identyfikatorem bazy danych, umożliwiając skojarzenie zrzutu ekranu z określonym rekordem bazy danych.
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")