I am using Mathematica to scrape information from webpages. To get file information, I am gathering plain text that is enclosed by <tr>
and </tr>
tags. I have made a list, of which each element is the entire data. I want to somehow scrape the plaintext readable element out of this.
By way of example, here are two elements from my list:
"<tr>
<td class=\"td_data\" valign=\"top\">1.</td>
<td class=\"td_data\" valign=\"top\">1841-1869 (Province of \
Canada), number 195, 21 June 1845, page 15</a></td>
<td class=\"td_data\" valign=\"top\"><a \
href=\"093/001060-119.01-e.php?image_id_nbr=2592&document_id_nbr=1857&\
f=g&PHPSESSID=kq2k6i3u6qodbjdp1ardk0ca96\">GIF</a> | <a \
href=\"093/001060-119.01-e.php?image_id_nbr=2592&document_id_nbr=1857&\
f=p&PHPSESSID=kq2k6i3u6qodbjdp1ardk0ca96\">PDF</a></td>
</tr>", "<tr>
<td class=\"td_data\" valign=\"top\">2.</td>
<td class=\"td_data\" valign=\"top\">1841-1869 (Province of \
Canada), number 402, Extra, 16 May 1849, page 4</a></td>
<td class=\"td_data\" valign=\"top\"><a \
href=\"093/001060-119.01-e.php?image_id_nbr=6979&document_id_nbr=2061&\
f=g&PHPSESSID=kq2k6i3u6qodbjdp1ardk0ca96\">GIF</a> | <a \
href=\"093/001060-119.01-e.php?image_id_nbr=6979&document_id_nbr=2061&\
f=p&PHPSESSID=kq2k6i3u6qodbjdp1ardk0ca96\">PDF</a></td>
</tr>"
I've been playing around with Shorten[]
and StringCases[]
to try to find a way, but would there be a way to quickly map a function on this list that would result in the following:
"1841-1869 (Province of Canada), number 195, 21 June 1845, page 15",
"1841-1869 (Province of Canada), number 402, Extra, 16 May 1849, page 4."
The actual text changes, i.e. it doesn't always say 1841-1869 etc., but the general format of the <tr>
portion remains consistent. I don't mind if the GIF | PDF part remains either. Would there be a quick way to just render the actually visible part of the HTML file in this list?
If somebody wants to reproduce the list I am using, here is my code (I include this only to make things simpler, but this is probably more of a theoretical question):
baseurl =
"http://www.collectionscanada.gc.ca/databases/canada-gazette/001060-\
110.01-e.php?q1=youth&q3=&interval=199&sk=";
pagelist =
Import[baseurl <> ToString[#], "Source"] & /@ Range[1, 1889, 199];
pagetext = Apply[StringJoin, pagelist];
trlist = StringCases[pagetext, Shortest["<tr>" ~~ ___ ~~ "</tr>"]];
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><><title//<p ltr<span></span</p></>
is a valid document), can't you get your hands on some XML version of the page? (I didn't even consider what horrible code the average page has, catchword quirks mode) $\endgroup$