Ferramentas para capturar e converter a Web

Capturar tabelas HTML de sites com Ruby

API Ruby

Convertendo tabelas HTML into JSON, CSV e planilhas do Excel usando API Ruby do GrabzIt é leste, basta seguir os exemplos mostrados aqui. No entanto, antes de começar, lembre-se de que depois de ligar para o url_to_table, html_to_table or file_to_table métodos os save or save_to O método deve ser chamado para capturar a tabela. Se você quiser ver rapidamente se este serviço é adequado para você, tente uma demonstração ao vivo da captura de tabelas HTML de um URL.

Opções Básicas

O exemplo abaixo converte a primeira tabela HTML em uma página da web especificada into um documento 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

Se você não deseja converter automaticamente a primeira tabela em uma página da web, pode especificar o tableNumberToInclude método. Por exemplo, especificar um 2 converteria a segunda tabela encontrada em uma página da web.

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

Você também pode especificar o targetElement O método que garantirá que apenas as tabelas dentro do ID do elemento especificado serão convertidas.

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

Se você usar o formato XLSX, poderá capturar todas as tabelas em uma página da Web passando true para o includeAllTables método. Isso colocará cada tabela em uma nova planilha na pasta de trabalho da planilha.

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

Converter tabelas HTML em JSON

Com o GrabzIt, o Ruby pode facilmente converter tabelas HTML into JSON para fazer isso especificar json no parâmetro format. No exemplo abaixo, os dados são lidos sincronicamente usando o save_to método, para obter o JSON como um string. Isso pode ser analisado por uma biblioteca como joia json.

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

Identificador Personalizado

Você pode passar um identificador personalizado para o mesa Como mostrado abaixo, esse valor é retornado ao seu manipulador GrabzIt Ruby. Por exemplo, esse identificador personalizado pode ser um identificador de banco de dados, permitindo que uma captura de tela seja associada a um registro específico do banco de dados.

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