怎么用R刮掉JAVASCRIPT渲染网站?
本教程将介绍如何用R刮掉JAVASCRIPT渲染网站?的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。
问题描述
我只想问一下,有什么什么好的方法来刮掉下面的网站?
https://list.jd.com/list.html?cat=737,794,798&page=1&sort=sort_rank_asc&trans=1&JL=6_0_0#J_main
基本上我想知道所有产品的名称和价格
但是,价格信息存储在一些JQuery脚本中
硒是唯一的解决方案吗?我想使用V8/Jsonlite,但似乎它们并不适用。如果你能在R中提供一些替代方案那就太好了。(我的电脑禁止访问exe文件,我不能使用Selify/PhantomJS]
推荐答案
找不到任何阻止抓取的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()
我们需要添加两个缺少的全局变量,然后计算该脚本:
paste0(
c("var window = {}, SEARCH = {};",
html_nodes(pg, "script")[[1]] %>%
html_text()
),
collapse = "
"
) %>%
ctx$eval()
## [1] "[object Object]"
现在获取一些数据:
ctx$get("aosList") %>%
bind_rows(.id = "id") %>%
tbl_df()
## # A tibble: 175 x 3
## idnv
## <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
## idIsSam 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 NANA 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>
好了关于怎么用R刮掉JAVASCRIPT渲染网站?的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。