I am looking for code that can inform me in which cell of an html table a particular icon resides. Here is what I am working with:
u <- "http://www.transfermarkt.nl/lionel-messi/leistungsdaten/spieler/28003/saison/2014/plus/1"
doc <- rvest::html(u)
tab <- rvest::html_table(doc, fill = TRUE)[[6]]
The column "Pos." designates the player's position in the field. Some of these have an additional icon. I can see the presence of these icons on the page as follows:
rvest::html_nodes(doc, ".kapitaenicon-table")
but this doesn't tell me WHERE they are. I would like my code to return that the icon occurs in rows 2, 10, 11, 27 of the "Pos. column" in the table. How can I do that?
A little bit more
rvest
and XPath magic can get you the indices:That gets the 6th table, extracts the
tr
s then looks through them for an 8thtd
with the properspan
/class
in it. If the XPath search fails it returns an empty list, so you can use the length to determine which rows have thetd
with the icon in them and which do not.This:
also works and it a bit tighter (and faster). It uses the XPath
boolean
operation to test for existence. This is handier if you have no other operations to perform on the node(s).This is an
xml2
version, though I have to believe there has to be a better way to do this inxml2
:UPDATE
For version
0.1.0.9000
ofxml2
I had to do the following:That should not be the case and I've filed a bug report.