youraz
youraz

Reputation: 483

How to scrape multiple classes using R?

I'm trying to get the prices from gittigidiyor.com but the problem is some classes are different if there is any discount. Normally, p class is "fiyat price-txt robotobold price" but in other case it is "fiyat robotobold price-txt".

Here is my code shown below:

    read_html(url) %>% 
          html_nodes("div p") %>% 
          html_nodes(xpath = '//*[@class="fiyat price-txt robotobold price"]') %>%  
          #html_nodes(xpath = '//*[@class="fiyat robotobold price-txt"]') %>% 
          html_text()

How can i add other class into my code and get all of them together "respectively"? Thanks in advance.

Upvotes: 0

Views: 1306

Answers (1)

QHarr
QHarr

Reputation: 84465

How can i add other class into my code and get all of them together "respectively"

Use css Or syntax

html_nodes('.fiyat.price-txt.robotobold.price, .fiyat.robotobold.price-txt'

That will match on either though, as in comments, you can try selecting for a single shared class. This is more robust than using multi-values.

e.g.

html_nodes('.price-txt, .price-txt')   # possibly just .robotobold or even just .fiyat  depending on rest of html

Upvotes: 4

Related Questions