簡體   English   中英

R:Rvest - 得到了我不想要的隱藏文字

[英]R: Rvest - got hidden text i don't want

我在網上搜索這個網頁:

http://www.falabella.com.pe/falabella-pe/category/cat40536/Climatizacion?navAction=push

我只需要產品中的信息:“品牌”,“產品名稱”,“價格”。

在此輸入圖像描述

我可以得到它,但我也從其他用戶的類似產品的橫幅中獲取信息。 我不需要它。

但是當我轉到頁面的源代碼時,我看不到那些產品。 我認為它是通過javascript或其他東西拉出來的:

在此輸入圖像描述

問題1: 如何在進行網絡抓取時阻止此信息? 這增加了我不需要的產品。 但是在源代碼中看不到這一部分。

問題2:當提取價格“precio1”時,我得到這個作為第一個元素: "\\n\\t\\t\\t\\tSubtotal InternetS/. 0"我在代碼源中也看不到它。 怎么不刮呢?

library(RSelenium)
library(rvest)
#start RSelenium
checkForServer()
startServer()
remDr <- remoteDriver()
remDr$open()

#navigate to your page
remDr$navigate("http://www.falabella.com.pe/falabella-pe/category/cat40536/Climatizacion?navAction=push")


page_source<-remDr$getPageSource()


Climatizacion_marcas1 <- html(page_source[[1]])%>%
        html_nodes(".marca") %>%
        html_nodes("a") %>%
        html_attr("title")


Climatizacion_producto1 <- html(page_source[[1]])%>%
        html_nodes(".detalle") %>%
        html_nodes("a") %>%
        html_attr("title")


Climatizacion_precio1 <- html(page_source[[1]])%>%
        html_nodes(".precio1") %>%
        html_text()

保持接近你的方法,這將做到:

library(rvest)
u <- "http://www.falabella.com.pe/falabella-pe/category/cat40536/Climatizacion?navAction=push"
doc <- html(u)

Climatizacion_marcas1 <- doc %>% 
  html_nodes(".marca")[[1]] %>%
  html_nodes("a") %>%
  html_attr("title")

Climatizacion_producto1 <- doc %>% 
  html_nodes(".detalle") %>%
  html_nodes("a") %>%
  html_attr("title")

“\\ n \\ t \\ t”等來自解析html。 顯然,那里有回車和標簽。 一個簡單的解決方案是

Climatizacion_precio1 <- doc %>% 
  html_node(".precio1") %>%
  html_text() %>% 
  stringr::str_extract_all("[:number:]{1,4}[.][:number:]{1,2}", simplify = TRUE) %>% 
  as.numeric

Climatizacion_precio1
[1] 44.9

實際上,這會從字符串中選取數字(因此也會刪除“S /。”。如果您希望保留“S /。”,則可以執行以下操作:

Climatizacion_precio1 <- doc %>% 
  html_node(".precio1") %>%
  html_text() %>% 
  gsub('[\r\n\t]', '', .)

Climatizacion_precio1
[1] "S/. 44.90"

編輯這是一種使用XMLselectr的替代方法。 這將一次性獲取頁面上所有項目的信息。

library(XML)

clean_up <- function(x) {
  stringr::str_replace_all(x, "[\r\t\n]", "")
}

product <- selectr::querySelectorAll(doc, ".marca") %>% 
  xmlApply(xmlValue) %>% lapply(clean_up) %>% unlist

details <-   selectr::querySelectorAll(doc, ".detalle a") %>% 
  xmlApply(xmlValue) %>% 
  unlist

price <- selectr::querySelectorAll(doc, ".precio1") %>% 
  xmlApply(xmlValue) %>% lapply(clean_up) %>% unlist

as.data.frame(cbind(product, details, price))
      product                  details      price
1       Imaco  Termoventilador NF15...  S/. 44.90
2       Imaco  Ventilador de 10"  I...     S/. 69
3       Imaco  Ventilador Imaco de ...     S/. 89
4      Taurus  Recirculador TRA-30 ...     S/. 89
5       Imaco  Termoventilador ITC-...    S/. 109
6        Sole Termo Ventilador Elé...     S/. 99
7      Taurus  Ventilador TVP-40 3 ...     S/. 99
8       Imaco  Estufa OFR7AO 1.500 ...    S/. 129
9      Alfano  Ventilador Recircula...    S/. 139
10     Taurus  Ventilador TVC-40RC ...    S/. 139
11      Imaco  Ventilador Pedestal ...    S/. 149
12     Alfano  Ventilador Orbital 1...    S/. 149
13 Electrolux  Ventilador  de Mesa ... S/. 149.90
14     Alfano  Estufa Termoradiador...    S/. 159
15     Alfano  Ventilador Pared 18"...    S/. 169
16      Imaco     Termoradiador OFR9AO    S/. 179

您通常可能希望對結果進行初步清理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM