有多种转换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")