我尝试使用rvest来获取该网站上的所有471个案例,但每次只能获得25个案例(无论列表是否扩展).任何帮助,将不胜感激.
library("rvest")
url <- "http://investmentpolicyhub.unctad.org/ISDS?status=100"
cases <- url %>%
read_html() %>%
html_nodes(xpath='//*[@id="cases-list"]') %>%
html_table()
View(cases)
Run Code Online (Sandbox Code Playgroud)
谢谢.
问题是你必须点击"全部显示"按钮来显示表格的其余部分,这是你无法用rvest做的.因此,使用RSelenium进行导航和rvest解析,
library(RSelenium)
library(rvest)
pJS <- phantom() # install PhantomJS if necessary
remDr <- remoteDriver(browserName = 'phantomjs')
remDr$open()
remDr$navigate(url)
button <- remDr$findElement(using = 'css selector', 'a#loadWholeList')
button$clickElement()
Sys.sleep(60) # or just wait a while, or rerun the following bits till they work
html <- remDr$getPageSource()
cases <- html[[1]] %>% read_html() %>%
html_node('table#cases-list') %>%
html_table()
if(nrow(cases) > 26){
remDr$close()
pJS$stop()
}
Run Code Online (Sandbox Code Playgroud)
因为您正在指导[无头]浏览器,所以单击后的位可能会给您25行或471行,具体取决于表是否已完成加载.该表非常大,因此加载需要一段时间,就像在普通浏览器中一样.如果你没有得到所有东西,请稍等一会儿Sys.sleep
再次运行.
但耐心一点:
tail(cases[, 1:3])
## No. Year of initiation Short casename
## 466 466 1995 Goetz v. Burundi (I)
## 467 467 1995 Leaf Tobacco v. Albania
## 468 468 1994 Gruslin v. Malaysia (I)
## 469 469 1994 Saar Papier v. Poland (I)
## 470 470 1993 AMT v. Zaire
## 471 471 1987 AAPL v. Sri Lanka
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
425 次 |
最近记录: |