polo
polo

Reputation: 185

How to get HTML element that is before a certain class?

I'm scraping and having trouble getting the element of the “th” tag that comes before the other “th” element that contains the “type2” class. I prefer to take it by identifying that it is the element "th" before the "th" with class "type2" because my HTML has a lot of "th" and that was the only difference I found between the tables.

Using rvest or xml2 (or other R package), can I get this parent? The content which I want is "text_that_I_want".

Thank you!

<tr>
    <th class="array">text_that_I_want</th>
    <td class="array">
        <table>
            <thead>
                <tr>
                    <th class="string type2">name</th>
                    <th class="array type2">answers</th>
                </tr>
            </thead>

Upvotes: 0

Views: 598

Answers (2)

Allan Cameron
Allan Cameron

Reputation: 174278

The formal and more generalizable way to navigate xpath relative to a given node is via ancestor preceding-sibling:

read_html(htmldoc) %>% 
html_nodes(xpath = "//th[@class = 'string type2']/ancestor::td/preceding-sibling::th") %>% 
html_text()
#> [1] "text_that_I_want"

Upvotes: 2

Ahorn
Ahorn

Reputation: 3876

We can look for the "type2" string in all <th>s, get the index of the first occurrence and substract 1 to get the index we want:

library(dplyr)
library(rvest)

location <- test%>% 
  html_nodes('th') %>% 
  str_detect("type2")

index_want <- min(which(location == TRUE) - 1)

test%>% 
  html_nodes('th') %>%
  .[[index_want]] %>% 
  html_text()

[1] "text_that_I_want"

Upvotes: 1

Related Questions