by Reinhard Max
This little script sends it's command line arguments as a query to the online dictionary at http://dict.leo.org and writes the parsed result to stdout. It uses Tcl's http package and the htmlparse and ncgi packages from Tcllib.
package require http package require htmlparse package require ncgi set table {{English German}} set max 0 proc max {a b} { expr {$a > $b ? $a : $b} } proc HTML {tag close options body} { switch -- $close$tag { TR {set ::TR ""} TD {set ::TD ""} /TR {if {[llength $::TR]} {lappend ::table $::TR }} /TD {if {[llength $::TD]} {lappend ::TR [join $::TD]}} default {append ::TD [string map { { }} $body]} } } proc main {argv} { set url "http://dict.leo.org/?search=[::ncgi::encode $argv]" set tok [::http::geturl $url] set data [::http::data $tok] ::http::cleanup $tok foreach line [split $data "\n"] { if {[string match "*search results*" $line]} break } ::htmlparse::parse -cmd HTML $line set max 0 foreach row $::table { foreach c $row {set max [max $max [string length $c]]} } incr max set sep [string repeat = $max] set ::table [linsert $::table 1 [list $sep $sep]] foreach row $::table { foreach {c1 c2} $row break puts [format "%-*s %-*s" $max $c1 $max $c2] } puts "" } main $argv
Web scraping | Using Tcl to write WWW client side applications