Giter Site home page Giter Site logo

dohliam / html-table2text Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 1.0 10 KB

HTML Table to Text - Extract and convert HTML tables to plain text formats

License: MIT License

Ruby 100.00%
html-tables html-converter html-conversion csv csv-converter tsv-converter csv-format markdown-converter html-to-markdown asciidoctor-converter

html-table2text's Introduction

HTML Table to Text - Extract and convert HTML tables to plain text formats

This is a simple (optionally interactive) script that can extract any or all tables from a given HTML file or URL. The data can be output to CSV (comma-separated values), TSV (tab-separated values), Markdown, Asciidoc, or raw HTML.

Requirements

This script relies on Nokogiri to parse HTML. You can install it with:

gem install nokogiri

Markdown conversion uses reverse_markdown which can be installed the same way:

gem install reverse_markdown

Asciidoc conversion uses the reverse_adoc gem:

gem install reverse_adoc

Usage

To extract tables from an arbitrary URL, just run the webtable_to_text script with the -u option followed by the URL:

./webtable_to_text.rb -u [URL]

For example:

./webtable_to_text.rb -u "https://en.wikipedia.org/wiki/Gabon"

This will print out all the tables found on the specified page.

To output a specific table only, use the n option, followed by the number of the table:

./webtable_to_text.rb -u "https://en.wikipedia.org/wiki/Gabon" -n 3

The script also works with local files, using the f option, e.g.:

./webtable_to_text.rb -f some_file.html

Interactive mode

To use interactive mode, add the -i option to the command and specify a URL or file as normal. For example:

./webtable_to_text.rb -u "https://en.wikipedia.org/wiki/Gabon" -i

This will print a message with the total number of tables found in the document. If you enter a number at the prompt, it will print the corresponding table. Otherwise, pressing ENTER or RETURN will print all tables found.

For example, pressing 3 will print something like the following:

Population in Gabon
Year	Million 
1950	0.5 
2000	1.2 
2016	2

To run tests, just enter the following command:

ruby tests.rb

Options

The following options are available:

  • -A, --all: Print all tables found on the specified page
  • -a, --asciidoc: Output in asciidoc/asciidoctor format
  • -c, --csv: Output in CSV / comma separated values format
  • -f, --file FILE: Specify HTML input file as source for extracting tables
  • -h, --help: Print help text
  • -i, --interactive: Interactive mode
  • -m, --markdown: Output in markdown format
  • -n, --number NUM: Print specific table number only; separate multiple numbers with commas
  • -o, --output FILE: Specify output file (default: output to STDOUT)
  • -r, --raw: Output raw table HTML
  • -t, --tsv: Output in TSV / tab separated values format (default)
  • -u, --url URL: Specify URL as source for extracting tables

To do

  • add options and non-interactive mode
  • output to raw HTML
  • output to Markdown
  • output to AsciiDoc

Credits

License

MIT.

html-table2text's People

Contributors

dohliam avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

ernstki

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.