Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.6k views
in Technique[技术] by (71.8m points)

r - Scraping with Rvest, POST request

I Have a problem with a request in Rvest.

I acces to this link from my browser and the elements appear while scrolling:

https://www.disco.com.ar/Comprar/Home.aspx#_atCategory=false&_atGrilla=true&_id=446223

Looking the details,I found this request:

enter image description here

(link2 = "https://www.disco.com.ar/Comprar/HomeService.aspx/ObtenerArticulosPorDescripcionMarcaFamiliaLevex")

Never use a "POST" as a request before. I tried these codes but was unsuccessful.

library(rvest)
library(httr)
library(jsonlite)


url_2<-"https://www.disco.com.ar/Comprar/HomeService.aspx/ObtenerArticulosPorDescripcionMarcaFamiliaLevex"

cuerpo <- list(
  
  IdMenu="446223",textoBusqueda="", producto="", marca="", pager="", ordenamiento=0, precioDesde="", precioHasta=""
)

page <- httr::POST(url_2,body=cuerpo,encode="form")

pagina <- content(page) %>% 
  html_attr("value")
r <- POST("https://www.disco.com.ar/Geolocalizacion/Geolocalizacion.aspx/GuardarLocalizacion", 
          content_type("application/json"),
          body = toJSON(
            list(
              latitud = NA,
              longitud = NA,
              noLocalizar = TRUE
            ), auto_unbox = TRUE
          ),encode = "json")

cookieList <- cookies(r) 
cookies <- cookieList$value %>% setNames(cookieList$name) 



url <- "https://www.disco.com.ar/Comprar/Home.aspx#_atCategory=false&_atGrilla=true&_id=446223"

main_page <- html_session(url,set_cookies(cookies))

How can I access the following data (response) (within the link2 above)?

"{"d":"{"Tipo":[{"Filtro":"Importada","IdMenu":"0","Cantidad":"17"},{"Filtro":"Otros","IdMenu"..."

Any help is welcome. Thank you so much!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The output you printed is a json string. You can parse it into a list/dataframe with jsonlite::fromJSON. The result will be a list and then you can look in it for the data you are searching for. I combined both your code samples into a simpler code.

library(httr)
library(rvest)
library(jsonlite)

r <- POST("https://www.disco.com.ar/Comprar/HomeService.aspx/ObtenerArticulosPorDescripcionMarcaFamiliaLevex", 
          content_type("application/json"),
          body = toJSON(list(IdMenu="446223",textoBusqueda="", producto="", marca="", pager="", ordenamiento=0, precioDesde="", precioHasta=""), 
                        auto_unbox = TRUE
          ), encode = "json")

data.json <- r %>% content() %>% nth(n=1) %>% fromJSON()

data.json is a list with 5 elements. Each element is a dataframe with data of different types.

For example data.json$ResultadosBusquedaLevex returns a dataframe with a list of products and some details (like price).

df.1 <- data.json$ResultadosBusquedaLevex
head(df.1)



  IdArticulo IdArchivoSmall IdArchivoBig                              DescripcionArticulo Precio PrecioAnterior precioUnidad_1 precioUnidad_0 Stock
1       4862     872107.jpg   872112.jpg                          Cerveza Heineken 473 Ml 110.33                            Lt         233.26    47
2       4867     893696.jpg   893701.jpg        Cerveza Rubia Quilmes Clásica 473 Ml Lata  72.10                            Lt         152.43    43
3       5239     893714.jpg   893719.jpg Cerveza Rubia Quilmes Clásica 6-Pack 473 Ml Lata 353.50                            Un          58.92    39
4      12710     872125.jpg   872130.jpg                          Cerveza Heineken 330 Ml  88.81                            Lt         269.12   458
5      84122     893776.jpg   893781.jpg           Cerveza Rubia Brahma Chopp 473 Ml Lata  79.27                            Lt         167.59    44
6     119170     893793.jpg   893798.jpg    Cerveza Rubia Brahma Chopp 6-Pack 473 Ml Lata 364.00                            Lt         769.56   443
  StockCD IdMenu Pesable CargarCaracteristicasDelProducto Grupo_Marca Grupo_Tipo Grupo_Precio Row Articulo_Oferta CucardaOferta CucardaOferta2
1       0      0   False                                     HEINEKEN      Otros            1   1               0                             
2       0      0   False                                      QUILMES      Otros            1   2               0                             
3       0      0   False                                      QUILMES      Otros            3   3               0                             
4       0      0   False                                     HEINEKEN      Otros            1   4               0                             
5       0      0   False                                       BRAHMA      Otros            1   5               0                             
6       0      0   False                                       BRAHMA      Otros            3   6               0                             
  CucardaOferta3 ArticuloEnCarrito ArticuloCantidadPedida Descuentos EsMacriProd SinInteres         Categoria UnidadMult MinCompra
1                                N                      0       NULL       FALSE            Bebidas->Cervezas          1         1
2                                N                      0       NULL       FALSE            Bebidas->Cervezas          1         1
3                                N                      0       NULL       FALSE            Bebidas->Cervezas          1         1
4                                N                      0       NULL       FALSE            Bebidas->Cervezas          1         1
5                                N                      0       NULL       FALSE            Bebidas->Cervezas          1         1
6                                N                      0       NULL       FALSE            Bebidas->Cervezas          1         1

    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...