您要查找的标记在您请求的文档中不存在.它是在页面加载后由javascript代码动态构建的.幸运的是,页面上确实存在实际数据,在其中一个脚本标记中以json字符串的形式存在.这需要进行一些分析,但包含您需要的所有信息:
library(rvest)
library(xml2)
library(tidyverse)
url <- read_html("https://www.walmart.com/store/finder?location=Missouri&distance=50")
stores <- html_element(url, xpath = "//script[@id='storeFinder']") %>%
html_text() %>%
jsonlite::parse_json()
do.call(rbind, lapply(stores$storeFinder$storeFinderCarousel$stores,
function(x) as.data.frame(x$address)))
#> postalCode address city state country
#> 1 65401 500 S Bishop Ave Rolla MO US
#> 2 65584 185 Saint Robert Blvd Saint Robert MO US
#> 3 65453 100 Ozark Dr Cuba MO US
#> 4 65560 1101 W Highway 32 Salem MO US
#> 5 65066 1888 Highway 28 Owensville MO US
#> 6 63080 350 Park Ridge Rd Sullivan MO US
#> 7 65101 401 Supercenter Dr Jefferson City MO US
#> 8 65065 4252 Highway 54 Osage Beach MO US
#> 9 65483 1433 S Sam Houston Blvd Houston MO US
#> 10 65109 724 Stadium West Blvd Jefferson City MO US
#> 11 65026 1802 S Business 54 Eldon MO US
#> 12 65020 94 Cecil St Camdenton MO US
#> 13 65536 1800 S Jefferson Ave Lebanon MO US