25

I am under Linux and I want to fetch an html page from the web and then output it on terminal. I found out that html2text essentially does the job, but it converts my html to a plain text whereas I would better convert it into ansi colored text in the spirit of ls --color=auto. Any ideas?

3 Answers 3

19

The elinks browser can do that. Other text browsers such as lynx or w3m might be able to do that as well.

elinks -dump -dump-color-mode 1 http://example.com/

the above example provides a text version of http://example.com/ using 16 colors. The output format can be customized further depending on need.

The -dump option enables the dump mode, which just prints the whole page as text, with the link destinations printed out in a kind of "email-style".

-dump-color-mode 1 enables the coloring of the output using the 16 basic terminal colors. Depending on the value and the capabilities of the terminal emulator this can be up to ~16 million (True Color). The values are documented in elinks.conf(5).

The colors used for output can be configured as well, which is documented in elinks.conf(5) as well.

1
  • Note that elinks does not automatically reset the terminal colors, resulting any subsequent output (from a script or terminal) to be the same color as the end of the elinks output until a new color code is encountered. You can reset manually by executing tput sgr0 after elinks.
    – Marijn
    Jan 8, 2018 at 16:12
0

The w3m browser supports coloring the output text.

5
  • 1
    well, lynx also does but I only need to output the page and not to browse it.
    – Sergey
    Mar 24, 2011 at 10:05
  • 1
    You don't have to. w3m has a -dump-like option too, and elinks does as well. It's just a matter of choosing the one you are most comfortable with.
    – davrieb
    Mar 24, 2011 at 16:00
  • 2
    @davrieb, any idea how to get one of the browsers to dump in color?
    – sarnold
    Mar 25, 2011 at 7:41
  • @sarmold: Not from the top of my head, but yes. I will provide details in a separate answer for better discoverability.
    – davrieb
    Mar 28, 2011 at 12:55
  • @davrieb, w3m [0.5.3, 2018] -dump suppresses colored output
    – fche
    Jul 4, 2019 at 17:54
0

You can use the lynx browser to output the text using this command.

lynx -dump http://example.com
2
  • 7
    Any idea how to get lynx -dump to also include color? At least on my distro, lynx is compiled against ncurses, and uses ncurses completely to determine when to use color; I couldn't get it to dump color output.
    – sarnold
    Mar 25, 2011 at 7:41
  • I tried lynx -dump somefile.html < <( echo -n qj) > out; cat out and variants thereof to no avail. Just to let you know that doesn’t work either. So unless you hack ncurses… But then installing elinks is probably less of a hassle again. ;)
    – anon
    Dec 11, 2013 at 23:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.