Il y a plusieurs façons de convertir des tableaux HTML into des feuilles de calcul CSV et Excel utilisant API Python de GrabzIt, détaillées voici quelques-unes des techniques les plus utiles. Cependant, avant de commencer, rappelez-vous qu'après avoir appelé le URLToTable, HTMLToTable or FileToTable méthodes le Save or SaveTo méthode doit être appelée pour capturer la table. Si vous voulez voir rapidement si ce service vous convient, vous pouvez essayer démonstration en direct de la capture de tableaux HTML à partir d'une URL.
L'extrait de code ci-dessous convertit automatiquement le premier tableau HTML d'une page Web spécifiée. into un document CSV pouvant être téléchargé ou analysé.
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
Par défaut, cela convertira le premier tableau identifié into une table. Toutefois, la seconde table d’une page Web peut être convertie en transmettant un 2 au tableNumberToInclude
attribuer.
from GrabzIt import GrabzItTableOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItTableOptions.GrabzItTableOptions() options.tableNumberToInclude = 2 grabzIt.URLToTable("https://www.tesla.com", options) # Then call the Save or SaveTo method grabzIt.SaveTo("result.csv")
from GrabzIt import GrabzItTableOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItTableOptions.GrabzItTableOptions() 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")
from GrabzIt import GrabzItTableOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItTableOptions.GrabzItTableOptions() options.tableNumberToInclude = 2 grabzIt.FileToTable("tables.html", options) # Then call the Save or SaveTo method grabzIt.SaveTo("result.csv")
Vous pouvez également spécifier le targetElement
attribut qui garantit que seules les tables de l'ID d'élément spécifié seront converties.
from GrabzIt import GrabzItTableOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItTableOptions.GrabzItTableOptions() options.targetElement = "stocks_table" grabzIt.URLToTable("https://www.tesla.com", options) # Then call the Save or SaveTo method grabzIt.SaveTo("result.csv")
from GrabzIt import GrabzItTableOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItTableOptions.GrabzItTableOptions() 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")
from GrabzIt import GrabzItTableOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItTableOptions.GrabzItTableOptions() options.targetElement = "stocks_table" grabzIt.FileToTable("tables.html", options) # Then call the Save or SaveTo method grabzIt.SaveTo("result.csv")
Sinon, vous pouvez capturer toutes les tables d’une page Web en passant true à la includeAllTables
attribut, cependant cela ne fonctionnera qu'avec les formats XLSX et JSON. Cette option placera chaque table dans une nouvelle feuille dans le classeur généré.
from GrabzIt import GrabzItTableOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItTableOptions.GrabzItTableOptions() options.format = 'xlsx' options.includeAllTables = True grabzIt.URLToTable("https://www.tesla.com", options) # Then call the Save or SaveTo method grabzIt.SaveTo("result.xlsx")
from GrabzIt import GrabzItTableOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItTableOptions.GrabzItTableOptions() options.format = 'xlsx' options.includeAllTables = 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")
from GrabzIt import GrabzItTableOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItTableOptions.GrabzItTableOptions() options.format = 'xlsx' options.includeAllTables = True grabzIt.FileToTable("tables.html", options) # Then call the Save or SaveTo method grabzIt.SaveTo("result.xlsx")
L'utilisation du service de conversion de tables HTML de Python et GrabzIt vous permet de convertir des tables HTML into JSON. Comme indiqué ci-dessous, la première étape consiste à spécifier json
dans le paramètre format. Nous obtenons alors le JSON string de manière synchrone les SaveTo
méthode, vous pouvez ensuite utiliser votre analyseur JSON préféré pour Python pour convertir le fichier JSON. string into un objet.
from GrabzIt import GrabzItTableOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItTableOptions.GrabzItTableOptions() options.format = "json" options.tableNumberToInclude = 1 grabzIt.URLToTable("https://www.tesla.com", options) json = grabzIt.SaveTo()
from GrabzIt import GrabzItTableOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItTableOptions.GrabzItTableOptions() 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()
from GrabzIt import GrabzItTableOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItTableOptions.GrabzItTableOptions() options.format = "json" options.tableNumberToInclude = 1 grabzIt.FileToTable("tables.html", options) json = grabzIt.SaveTo()
Vous pouvez transmettre un identifiant personnalisé au table Comme indiqué ci-dessous, cette valeur est ensuite renvoyée à votre gestionnaire GrabzIt Python. Par exemple, cet identifiant personnalisé pourrait être un identifiant de base de données, permettant d'associer une capture d'écran à un enregistrement de base de données particulier.
from GrabzIt import GrabzItTableOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItTableOptions.GrabzItTableOptions() options.customId = "123456" grabzIt.URLToTable("https://www.tesla.com", options) # Then call the Save method grabzIt.Save("http://www.example.com/handler.py")
from GrabzIt import GrabzItTableOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItTableOptions.GrabzItTableOptions() 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.py")
from GrabzIt import GrabzItTableOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItTableOptions.GrabzItTableOptions() options.customId = "123456" grabzIt.FileToTable("example.html", options) # Then call the Save method grabzIt.Save("http://www.example.com/handler.py")