如何使用rvest从网页检索标题和价格

问题描述 投票:0回答:1

我正在使用

R
rvest
进行网页抓取任务。

我正在尝试从商店网页获取智能手机的名称及其价格。

我正在使用下一个代码:

library(rvest)
library(dplyr)
#Code
first_page <- read_html('https://catalogo.claro.com.ec/postpago/catalogo?utm_source=catalogo&utm_medium=menu-header&utm_campaign=equipos-celulares&utm_content=navigation')
links <- first_page %>% html_nodes(xpath="//*[@id='catalogo--productos']/div/div/section/article[1]/div/h3") %>%
  html_text()

当我尝试以前的代码时,我得到了空

character(0)
links

我已经从网页上查看了源代码:

并根据它设置xpath但它不起作用。有没有办法使用

rvest
获取每部智能手机的标题和价格?

非常感谢。

r rvest
1个回答
0
投票

我对 {rvest} 的尝试:

url <- "https://catalogo.claro.com.ec/postpago/catalogo?utm_source=catalogo&utm_medium=menu-header&utm_campaign=equipos-celulares&utm_content=navigation"

ses <- rvest::read_html_live(url)

ses$view() 

phones <- ses |>
  rvest::html_elements(xpath = '//*[@id="catalogo--productos"]/div/div/section') |>
  rvest::html_elements("h3") |>
  rvest::html_text()
  
prices <- ses |>
  rvest::html_elements(".price-new") |>
  rvest::html_text()

cbind(phones, prices)
#>       phones                                                                                           
#>  [1,] "MOTOROLA EDGE NEO 40 (256GB)"                                                                   
#>  [2,] "SAMSUNG GALAXY S24 ULTRA (512GB) + BUDS 2"                                                      
#>  [3,] "SAMSUNG GALAXY S24 PLUS (512GB) + BUDS 2"                                                       
#>  [4,] "SAMSUNG GALAXY S24 (256GB) + BUDS 2 ONYX + TRAVEL ADAPTER 25W"                                  
[...]
#> [73,] "IPHONE 11 (64GB)"                                                                               
#>       prices     
#>  [1,] "$ 453,60" 
#>  [2,] "$ 1868,44"
#>  [3,] "$ 1286,88"
#>  [4,] "$ 1062,88"
[...]
#> [73,] "$ 588,00"

创建于 2024-02-20,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.