Home > Net >  Rvest: select content from Xpath
Rvest: select content from Xpath

Time:10-09

I have the following source code:

<div class="re__section-body re__detail-content js__section-body js__pr-description js__tracking" trackingid="lead-phone-ldp" trackinglabel="loc=Sale-Listing Details-body,prid=30449316">
khu vực dao động từ 45tr - 60tr/m2.<br>Liên hệ tôi chính chủ SĐT: 
<span class="hidden-phone hidden-mobile m-cover js__btn-tracking" tracking-id="lead-phone-ldp" tracking-label="loc=Sale-Listing Details-body,prid=30449316" raw="0935686566">0935686***</span>.
</div>

I try to extract raw element in when using this code but this get null result

html_nodes(xpath = "//span[@class = 'hidden-phone hidden-mobile m-cover js__btn-tracking'") %>% 
html_attr("raw") %>% 
html_text()

How Can I do that? Thank all very much

CodePudding user response:

Let foo.html be your html file you want to extract node attributes from:

library(rvest)
library(magrittr)

read_html("foo.html") %>%
  html_nodes(xpath = "//span[@class='hidden-phone hidden-mobile m-cover js__btn-tracking']") %>%
  html_attr("raw") %>%
  as.numeric()

html_attr("raw") returns already a character so there is no need for subsequent html_text().

  • Related