找不到任何禁止抓取的 robots.txt 或条款/条件(如果有人发现,请在评论中标记,以便我删除答案):
library(rvest)
library(V8)
library(tidyverse)
pg <- read_html("https://list.jd.com/list.html?cat=737,794,798&page=1&sort=sort_rank_asc&trans=1&JL=6_0_0#J_main")
将问题标记为V8
是一个????????的主意。
ctx <- v8()
我们需要添加两个缺失的全局变量,然后评估 javascript:
paste0(
c("var window = {}, SEARCH = {};",
html_nodes(pg, "script")[[1]] %>%
html_text()
),
collapse = "\n"
) %>%
ctx$eval()
## [1] "[object Object]"
现在取出一些数据:
ctx$get("aosList") %>%
bind_rows(.id = "id") %>%
tbl_df()
## # A tibble: 175 x 3
## id n v
## <chr> <chr> <chr>
## 1 1429810 39-45英寸 244_110017
## 2 1429810 全高清(1920×1080) 3613_77848
## 3 1429810 3级 1200_1656
## 4 4286570 39-45英寸 244_110017
## 5 4286570 高清(1366×768) 3613_93579
## 6 4286570 3级 1200_1656
## 7 4609652 55英寸 244_1486
## 8 4609652 4k超高清(3840×2160) 3613_77847
## 9 4609652 3级 1200_1656
## 10 4609660 65英寸 244_58269
## # ... with 165 more rows
并且,更多数据:
ctx$get("attrList") %>%
bind_rows(.id = "id") %>%
tbl_df()
## # A tibble: 60 x 15
## id IsSam cw factoryShip isCanUseDQ isJDexpress isJX isOverseaPurchase mcat3Id soldOS tssp venderType xgzs
## <chr> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <chr> <chr>
## 1 1429810 0 1 0 0 0 0 0 798 -1 0 0 7.3
## 2 4286570 0 1 NA 0 0 0 0 798 -1 0 0 6.2
## 3 4609652 0 1 NA 0 0 0 0 798 -1 0 0 7.5
## 4 4609660 0 1 NA 0 0 0 0 798 -1 0 0 8.8
## 5 4620979 0 1 NA 0 0 0 0 798 -1 0 0 6.4
## 6 4751739 0 1 NA 1 0 0 0 798 -1 0 0 8.9
## 7 4902977 0 1 NA NA 0 0 0 798 -1 0 0 9.5
## 8 5010925 0 1 NA 1 0 0 0 798 -1 0 0 8.6
## 9 5102214 0 1 NA 0 0 0 0 798 -1 0 0 7.8
## 10 5218185 0 1 NA 1 0 0 0 798 -1 0 0 <NA>
## # ... with 50 more rows, and 2 more variables: isFzxp <int>, shipFareTmplId <int>