When a service API is not available, sometimes the only recourse is to scrape a web interface instead. An alternative to is to work with the web host to work out details of a Web Service that would provide useful information programatically.
Web scraping is often employed for small tasks where an API, such as sendi updates to a pager/WAP phone, etc., emailing one's personal account status to an account, or moving data from a website into a local database.
"Web Scraping ..." [L2 ] (URL not 404 - instead links to a generic landing page on Aug 28, 2011)
Etiquette
NEM notes that to be a good web-citizen any web robots should follow some guidelines:
set the user-agent to some unique identifier for your program and include some contact details (email, website) so that site admins can contact you if they have any issues (you can do this with http::config -useragent);
fetch the /robots.txt file from the server you are scraping and check every URL accessed against it (see [L3 ]).
(probably more - feel free to expand this list). Checking the robots.txt is relatively simple but still requires a bit of effort. As I'm currently doing some web-scraping I may package up the code I'm using for this into a webrobot package.
tcllib/examples/oscon: which uses the htmlparse module (among others) to parse the schedule pages for OSCON 2001 and convert them into CSV files usable by Excel and other applications. LemonTree branch uses this technique.
LV This isn't technically web scraping, but I'm uncertain where else to reference it - it is making use of a web site's cgi functionality, from a Tk application, from what I can tell
RS: loads an RSS page, renders it to HTML, plus it compacts the referenced pages into the same document with local links, while trying to avoid ads and noise.
Not perfect, but I use it daily to reap news sites onto my iPaq :)
Apt comments on the technical and business difficulty of Web scraping, along with mentions of WebL and NQL, appear here [L4 ].
Perl probably has the most current Web-scraping activity (even more than tclwebtest?), especially with WWW::Mechanize , although Curl also has its place.
In 2004, WWWGrab [L5 ] looks interesting for those in a position to work under Windows.
in which Louis Brandy describes using Python and iMacros to do some web scraping
Example by Ian, 2007, comp.lang.tcl
LV 2007-11-01: On comp.lang.tcl, during Oct 31, 2007, in a thread [L6 ] about someone wanting to extract data from an html page, a user by the name of Ian posted the following snippet of code as an example of what they do to deal with a page of html that has some data in it that
package require htmlparse
package require struct
proc html2data s {
::struct::tree x
::htmlparse::2tree $s x
::htmlparse::removeVisualFluff x
set data [list]
x walk root q {
if {([x get $q type] eq "PCDATA") &&
[string match R\u00e6kke/pulje [x get $q data]]} {
set p $q
for {set i 3} {$i} {incr i -1} {set p [x parent $p]}
foreach {row} [lrange [x children $p] 1 end] {
......
}
break
}
}
return $data
}
[It seems many of us do our own "home-grown" solutions for these needs.]
Also, some web hosts provide XML versions (RSS), or specially formatted versions for use with Avantgo or the plucker command, with the goal of aiding people who need some sort of specialized format for small devices, etc.
It would be great if someone making legitimate use of some of these sources would share some of their code to do this sort of thing.