Tools zum Erfassen und Konvertieren des Webs

Erfassen Sie HTML-Tabellen von Websites mit Ruby

Ruby API

HTML-Tabellen konvertieren into JSON-, CSV- und Excel-Tabellen mit GrabzIt's Ruby API Im Osten folgen Sie einfach den hier gezeigten Beispielen. Bevor Sie jedoch anfangen, denken Sie daran, dass nach dem Aufruf der url_to_table, html_to_table or file_to_table Methoden der save or save_to Es muss eine Methode aufgerufen werden, um die Tabelle zu erfassen. Wenn Sie schnell herausfinden möchten, ob dieser Service für Sie geeignet ist, können Sie a Live-Demo zum Erfassen von HTML-Tabellen von einer URL.

Grundlegende Optionen

Im folgenden Beispiel wird die erste HTML-Tabelle in einer angegebenen Webseite konvertiert into ein CSV-Dokument.

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

Wenn Sie die erste Tabelle auf einer Webseite nicht automatisch konvertieren möchten, können Sie die angeben tableNumberToInclude Methode. Zum Beispiel würde das Angeben eines 2 die zweite in einer Webseite gefundene Tabelle konvertieren.

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")

Sie können auch angeben targetElement Methode, die sicherstellt, dass nur Tabellen innerhalb der angegebenen Element-ID konvertiert werden.

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")

Wenn Sie das XLSX-Format verwenden, können Sie alle Tabellen auf einer Webseite erfassen, indem Sie true an übergeben includeAllTables Methode. Dadurch wird jede Tabelle in ein neues Blatt in der Arbeitsmappe der Kalkulationstabelle eingefügt.

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")

Konvertieren Sie HTML-Tabellen in JSON

Mit GrabzIt kann Ruby problemlos HTML-Tabellen konvertieren into JSON, um dies anzugeben json im Formatparameter. Im folgenden Beispiel werden die Daten gelesen synchron mit dem save_to Methode, um die JSON als string. Dies kann dann von einer Bibliothek wie folgt analysiert werden json gem.

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()

Benutzerdefinierte Kennung

Sie können der einen benutzerdefinierten Bezeichner übergeben Tabelle Bei den folgenden Methoden wird dieser Wert an Ihren GrabzIt Ruby-Handler zurückgegeben. Diese benutzerdefinierte Kennung kann beispielsweise eine Datenbankkennung sein, mit der ein Screenshot einem bestimmten Datenbankeintrag zugeordnet werden kann.

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")