Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html R从web提取表,不带<;span class=";“隐藏的”&燃气轮机;_Html_R - Fatal编程技术网

Html R从web提取表,不带<;span class=";“隐藏的”&燃气轮机;

Html R从web提取表,不带<;span class=";“隐藏的”&燃气轮机;,html,r,Html,R,我正试图取消本周末举行的波兰选举的结果,但我遇到了一个问题,即在加入每一个随机浮动之前 我尝试过使用htmltab,但它不起作用-正如您所看到的,添加了随机数 library(htmltab) url <- "https://wybory2018.pkw.gov.pl/pl/geografia/020000#results_vote_council" tmp <- htmltab::htmltab(doc = html, which = 1) tmp Wyszczególni

我正试图取消本周末举行的波兰选举的结果,但我遇到了一个问题,即在加入每一个随机浮动之前

我尝试过使用
htmltab
,但它不起作用-正如您所看到的,添加了随机数

library(htmltab)
url <- "https://wybory2018.pkw.gov.pl/pl/geografia/020000#results_vote_council"
tmp <- htmltab::htmltab(doc = html, which = 1) 
tmp

  Wyszczególnienie              Liczba
2      Mieszkańców 0.972440432 755 957
3         Wyborców 0.977263472 273 653
4          Obwodów      0.99998061 940
编辑2 我没有找到最优雅的解决方案:

#https://stackoverflow.com/questions/7963898/extracting-the-last-n-characters-from-a-string-in-r
substrRight <- function(x, n){
  substr(x, nchar(x)-n+1, nchar(x))
}

tmp <- htmltab::htmltab(doc = html, which = 9) 

tmp2 <- xml2::read_html(html) %>%
  rvest::html_nodes("tbody") %>%
  magrittr::extract2(9)  %>%
  rvest::html_nodes("tr") %>%
  rvest::html_nodes("td") %>%
  rvest::html_nodes("span") %>%
  rvest::html_text() %>%
  matrix(ncol = 4, byrow = T) %>%
  data.frame() 

names(tmp) <- c("a", "b", "c", "d", "e", "f", "g")
tmp3 <- cbind(tmp, tmp2) %>%
  mutate(n_to_delate = nchar(X1),
         c1 = as.character(c),
         n_whole = nchar(c1),
         c2 = substrRight(c1, n_whole - n_to_delate),
         c3 = gsub(" ", "", c2),
         c4 = as.numeric(c3)) %>%
  select(b, c4)

names(tmp3) <- c("party", "n_of_votes")
#https://stackoverflow.com/questions/7963898/extracting-the-last-n-characters-from-a-string-in-r
次右侧%
rvest::html_节点(“tr”)%>%
rvest::html_节点(“td”)%>%
rvest::html_节点(“span”)%>%
rvest::html_text()%>%
矩阵(ncol=4,byrow=T)%>%
data.frame()
名称(tmp)%
选择(b、c4)
解决原始问题的名称(tmp3):
可以在转换为表之前删除这些节点:

library(rvest)

pg <- read_html("https://wybory2018.pkw.gov.pl/pl/geografia/020000#results_vote_council")

tbl_1 <- html_nodes(pg, xpath=".//table[@class = 'stat_table']")[1]

xml_remove(html_nodes(tbl_1, xpath=".//span[@class='hidden']"))

html_table(tbl_1)
## [[1]]
##   Wyszczególnienie    Liczba
## 1      Mieszkańców 2 755 957
## 2         Wyborców 2 273 653
## 3          Obwodów     1 940
现在,我们需要知道有多少真正的列,因为它有一个愚蠢的标题,它是多行的,带有跨越多个列的

length(
  html_nodes(target_tbl, xpath=".//tbody/tr[1]") %>% 
    html_nodes("td")
) -> n_cols
现在,我们拉出每一列,设置好列名,将其转换为数据帧,并删除仅输入填充条的垃圾列:

as.data.frame(
  setNames(
    lapply(1:n_cols, function(.idx) {
      html_nodes(target_tbl, xpath=sprintf(".//tbody/tr/td[%s]", .idx)) %>% 
        html_text(trim=TRUE)
    }),
    c(
      "nr_listy", "komitet_wyborczy", "głosów_na_kandydatów_komitetu", 
      "kandydatów", "mandatów", "pct_głosów_ważnych", "junk", 
      "udział_w_podziale_mandatów"
    )
  ),
  stringsAsFactors = FALSE
) -> xdf

xdf$junk <- NULL

str(xdf)
## 'data.frame': 12 obs. of  7 variables:
##  $ nr_listy                     : chr  "1" "2" "3" "4" ...
##  $ komitet_wyborczy             : chr  "KOMITET WYBORCZY WYBORCÓW BEZPARTYJNI SAMORZĄDOWCY" "KOMITET WYBORCZY POLSKIE STRONNICTWO LUDOWE" "KOMITET WYBORCZY PARTIA ZIELONI" "KOALICYJNY KOMITET WYBORCZY PLATFORMA.NOWOCZESNA KOALICJA OBYWATELSKA" ...
##  $ głosów_na_kandydatów_komitetu: chr  "168 442" "58 820" "19 783" "289 831" ...
##  $ kandydatów                   : chr  "46" "46" "36" "46" ...
##  $ mandatów                     : chr  "6" "1" "0" "13" ...
##  $ pct_głosów_ważnych           : chr  "14.98%" "5.23%" "1.76%" "25.77%" ...
##  $ udział_w_podziale_mandatów   : chr  "Tak" "Tak" "Nie" "Tak" ...
解决原始问题: 可以在转换为表之前删除这些节点:

library(rvest)

pg <- read_html("https://wybory2018.pkw.gov.pl/pl/geografia/020000#results_vote_council")

tbl_1 <- html_nodes(pg, xpath=".//table[@class = 'stat_table']")[1]

xml_remove(html_nodes(tbl_1, xpath=".//span[@class='hidden']"))

html_table(tbl_1)
## [[1]]
##   Wyszczególnienie    Liczba
## 1      Mieszkańców 2 755 957
## 2         Wyborców 2 273 653
## 3          Obwodów     1 940
现在,我们需要知道有多少真正的列,因为它有一个愚蠢的标题,它是多行的,带有跨越多个列的

length(
  html_nodes(target_tbl, xpath=".//tbody/tr[1]") %>% 
    html_nodes("td")
) -> n_cols
现在,我们拉出每一列,设置好列名,将其转换为数据帧,并删除仅输入填充条的垃圾列:

as.data.frame(
  setNames(
    lapply(1:n_cols, function(.idx) {
      html_nodes(target_tbl, xpath=sprintf(".//tbody/tr/td[%s]", .idx)) %>% 
        html_text(trim=TRUE)
    }),
    c(
      "nr_listy", "komitet_wyborczy", "głosów_na_kandydatów_komitetu", 
      "kandydatów", "mandatów", "pct_głosów_ważnych", "junk", 
      "udział_w_podziale_mandatów"
    )
  ),
  stringsAsFactors = FALSE
) -> xdf

xdf$junk <- NULL

str(xdf)
## 'data.frame': 12 obs. of  7 variables:
##  $ nr_listy                     : chr  "1" "2" "3" "4" ...
##  $ komitet_wyborczy             : chr  "KOMITET WYBORCZY WYBORCÓW BEZPARTYJNI SAMORZĄDOWCY" "KOMITET WYBORCZY POLSKIE STRONNICTWO LUDOWE" "KOMITET WYBORCZY PARTIA ZIELONI" "KOALICYJNY KOMITET WYBORCZY PLATFORMA.NOWOCZESNA KOALICJA OBYWATELSKA" ...
##  $ głosów_na_kandydatów_komitetu: chr  "168 442" "58 820" "19 783" "289 831" ...
##  $ kandydatów                   : chr  "46" "46" "36" "46" ...
##  $ mandatów                     : chr  "6" "1" "0" "13" ...
##  $ pct_głosów_ważnych           : chr  "14.98%" "5.23%" "1.76%" "25.77%" ...
##  $ udział_w_podziale_mandatów   : chr  "Tak" "Tak" "Nie" "Tak" ...

谢谢问题是我需要第9个元素,用你的方法我只能得到前两个元素。我已经尝试了
tmp2,看看这是否有助于更好地瞄准它。谢谢!问题是我需要第9个元素,用你的方法我只能得到前两个元素。我尝试了
tmp2,看看这是否有助于更好地定位它。
length(
  html_nodes(target_tbl, xpath=".//tbody/tr[1]") %>% 
    html_nodes("td")
) -> n_cols
as.data.frame(
  setNames(
    lapply(1:n_cols, function(.idx) {
      html_nodes(target_tbl, xpath=sprintf(".//tbody/tr/td[%s]", .idx)) %>% 
        html_text(trim=TRUE)
    }),
    c(
      "nr_listy", "komitet_wyborczy", "głosów_na_kandydatów_komitetu", 
      "kandydatów", "mandatów", "pct_głosów_ważnych", "junk", 
      "udział_w_podziale_mandatów"
    )
  ),
  stringsAsFactors = FALSE
) -> xdf

xdf$junk <- NULL

str(xdf)
## 'data.frame': 12 obs. of  7 variables:
##  $ nr_listy                     : chr  "1" "2" "3" "4" ...
##  $ komitet_wyborczy             : chr  "KOMITET WYBORCZY WYBORCÓW BEZPARTYJNI SAMORZĄDOWCY" "KOMITET WYBORCZY POLSKIE STRONNICTWO LUDOWE" "KOMITET WYBORCZY PARTIA ZIELONI" "KOALICYJNY KOMITET WYBORCZY PLATFORMA.NOWOCZESNA KOALICJA OBYWATELSKA" ...
##  $ głosów_na_kandydatów_komitetu: chr  "168 442" "58 820" "19 783" "289 831" ...
##  $ kandydatów                   : chr  "46" "46" "36" "46" ...
##  $ mandatów                     : chr  "6" "1" "0" "13" ...
##  $ pct_głosów_ważnych           : chr  "14.98%" "5.23%" "1.76%" "25.77%" ...
##  $ udział_w_podziale_mandatów   : chr  "Tak" "Tak" "Nie" "Tak" ...
lapply(1:n_cols, function(.idx) {
  html_nodes(target_tbl, xpath=sprintf(".//tbody/tr/td[%s]", .idx)) %>% 
    html_text(trim=TRUE)
}) %>% 
  setNames(c(
    "nr_listy", "komitet_wyborczy", "głosów_na_kandydatów_komitetu", 
    "kandydatów", "mandatów", "pct_głosów_ważnych", "junk", 
    "udział_w_podziale_mandatów"
  )) %>% 
  as.data.frame(stringsAsFactors = FALSE) -> xdf