有多種轉換HTML表的方法 into使用CSV和Excel電子表格 GrabzIt的Python API,這裡詳細介紹了一些最有用的技術。 但是,在開始之前,請記住 URLToTable, HTMLToTable or 文件到表格 方法 Save or SaveTo 必須調用方法來捕獲表。 如果您想快速查看此服務是否適合您,可以嘗試 捕獲HTML表的實時演示 從URL。
以下代碼段自動轉換指定網頁中的第一個HTML表 int可以下載或解析的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
默認情況下,它將轉換它標識的第一個表 intoa表。 但是,可以通過將2傳遞給網頁中的第二個表來進行轉換 tableNumberToInclude
屬性。
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")
您還可以指定 targetElement
該屬性將確保僅轉換指定元素ID中的表。
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")
另外,您也可以通過將true傳遞給 includeAllTables
屬性,但是僅適用於XLSX和JSON格式。 此選項會將每個表放在生成的電子表格工作簿中的新表中。
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")
使用Python和GrabzIt的HTML表格轉換服務可以轉換HTML表格 into JSON。 如下所示的第一步是指定 json
在format參數中。 然後我們得到JSON string 同步地 與 SaveTo
方法,然後您可以使用自己喜歡的Python JSON解析器來轉換JSON string intoa對象。
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()
您可以將自定義標識符傳遞給 表 方法,如下所示,然後將該值返回給您的GrabzIt Python處理程序。 例如,此自定義標識符可以是數據庫標識符,從而允許將屏幕截圖與特定的數據庫記錄相關聯。
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")