尝试使用rvest
抓取this site投票结果。使用选择器小工具
这看起来像是正确的节点 - 所以我尝试了以下内容:
library(plyr)
library(rvest)
library(stringr)
library(magrittr)
res <- "http://www.politico.com/2016-election/results/map/president/alabama/" %>%
read_html
我将需要节点的县名,所以我先把它们刮掉
cnames <- res %>%
html_nodes("h4") %>%
html_text %>%
extract(str_detect(., "County"))
然后通过llply
电话
cnames %>%
str_sub(, str_locate(., fixed(" "))[, 1] - 1) %>%
str_c("#county",
.,
" .results-popular") %>%
llply(failwith(NULL,
function(i)
res %>%
html_nodes(i) %>%
html_text))
但是结果对象只提供了10组结果:
[[1]]
[1] "0" "0" "0" "0"
[[2]]
[1] "0" "0" "0" "0"
[[3]]
[1] "0" "0" "0" "0"
[[4]]
[1] "0" "0" "0" "0"
[[5]]
[1] "0" "0" "0" "0"
[[6]]
[1] "0" "0" "0" "0"
[[7]]
[1] "0" "0" "0" "0"
[[8]]
[1] "0" "0" "0" "0"
[[9]]
[1] "0" "0" "0" "0"
[[10]]
[1] "0" "0" "0" "0"
[[11]]
character(0)
[[12]]
character(0)
如何刮掉剩余的结果?