Rvest html_table 错误 - out[j + k, ] : subscript out of bounds 中的错误

标签 r screen-scraping rvest

我对用 R 抓取有点陌生,但我收到一条我无法理解的错误消息。我的代码:

 url <- "https://en.wikipedia.org/wiki/California_State_Legislature,_2017%E2%80%9318_session"

leg <- read_html(url)

testdata <- leg %>% 
  html_nodes('table') %>% 
  .[6] %>% 
  html_table()

我得到的回应是:

Error in out[j + k, ] : subscript out of bounds



当我用 html_text 换出 html_table 时,我没有收到错误消息。知道我做错了什么吗?

谢谢!

最佳答案

希望这可以帮助!

library(htmltab)
library(dplyr)
library(tidyr)

url <- "https://en.wikipedia.org/wiki/California_State_Legislature,_2017%E2%80%9318_session"
url %>%
  htmltab(6, rm_nodata_cols = F) %>%
  .[,-1] %>%
  replace_na(list(Notes = "", "Term-limited?" = "")) %>%
  `rownames<-` (seq_len(nrow(.)))

输出是:
  District              Name      Party       Residence Term-limited? Notes
1        1        Ted Gaines Republican El Dorado Hills                    
2        2      Mike McGuire Democratic      Healdsburg                    
3        3         Bill Dodd Democratic            Napa                    
4        4       Jim Nielsen Republican          Gerber                    
5        5 Cathleen Galgiani Democratic        Stockton                    
6        6       Richard Pan Democratic      Sacramento                    
...

关于Rvest html_table 错误 - out[j + k, ] : subscript out of bounds 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47585699/

相关文章:

python - 网页抓取谷歌财经

使用 <a href="javascript :linkTo_UnCryptMailto(%27ocknvq%2Cjgkmg0qdgtnkpBwpk%5C%2Fvwgdkpigp0fg%27) 接收邮件

r - 使用 data.table 根据特定日期条件过滤观察结果

r - 如何从R中的矩阵列表中的每个矩阵中删除列?

r - 使用 transition_time 随时间 gganimate,其中时间变量是 NBA 比赛时钟

c# - 单元测试屏幕刮刀

c# - 已经运行的读取命令 (CMD) 窗口

r - 如何将 xpath 传递给 html_nodes()?

read_html 不从简单的 html 页面检索所有数据,而是返回不完整的 html?

r - 在 R 中创建唯一 ID 列