如何使用R抓取多个类?

时间:2019-09-16 09:18:54

标签: r rvest

我正在尝试从gittigidiyor.com获取价格,但问题是,如果有折扣,某些舱位会有所不同。通常,p类是“ fiyat价格-文本robotobold价格”,但在其他情况下,它是“ fiyat价格-文本robotobold价格”。

这是我的代码,如下所示:

    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()

如何将其他类添加到我的代码中并“分别”将它们全部聚集在一起? 预先感谢。

1 个答案:

答案 0 :(得分:1)

  

如何将其他类添加到我的代码中,并将所有类放在一起   “分别”

使用CSS或语法

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

尽管两者都可以匹配,如注释中所示,您可以尝试选择单个共享类。这比使用多值更可靠。

例如

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