rvest,带有AD和tbody标签的桌子

rvest,带有AD和tbody标签的桌子,r,rvest,R,Rvest,我正在慢慢地用rvest学习拉网。我想刮掉下表 我主要对第一列感兴趣,但我不介意获得整个表 我尝试了两种可能的方法。仅解析头部的最简单方法: url <- 'https://novostavby.com/cs/developery/' read_html(x=url) %>% html_nodes('table') %>% html_table 但它返回的标题只是不知道为什么 谢谢你的帮助 您的url指向包含空表的html页面。在web浏览器中可以看到表格内容的原

我正在慢慢地用rvest学习拉网。我想刮掉下表 我主要对第一列感兴趣,但我不介意获得整个表

我尝试了两种可能的方法。仅解析头部的最简单方法:

url <- 'https://novostavby.com/cs/developery/'
read_html(x=url) %>% 
  html_nodes('table') %>% 
  html_table
但它返回的标题只是不知道为什么


谢谢你的帮助

您的url指向包含空表的html页面。在web浏览器中可以看到表格内容的原因是html指示浏览器从其他页面下载表格内容并将其插入空表格中。当然,只读取第一页的html,而不运行javascript加载表数据

在您的例子中,数据是从指向JSON文件的另一个url加载的。实际上,可以将其内容插入原始html并使用rvest获取表。这实际上是手动执行浏览器的操作

require(httr)
require(magrittr)
require(rvest)

# Get the page's html as text
url <- 'https://novostavby.com/cs/developery/'
original_page <- GET(url) %>% content("text") 

# Get the JSON as plain text from the link generated by Javascript on the page
json_url <- "https://novostavby.com/ajax-estatio-developers/?citypath=undefined&sortdir=asc&sortfield=title&search=&pagefrom=developers"
JSON <- GET(json_url) %>% content("text", encoding = "utf8") 

# Remove the double escapes and enclosing brackets / html key from the JSON
# to get its html contents
table_contents <- JSON     %>%
 {gsub("\\\\n", "\n", .)}  %>%
 {gsub("\\\\/", "/", .)}   %>%
 {gsub("\\\\\"", "\"", .)} %>%
  strsplit("html\":\"")    %>%
  unlist                   %>%
  extract(2)               %>%
  substr(1, nchar(.) -2)   %>% 
  paste0("</tbody>")

# insert the table html into the original page
new_page <- gsub("</tbody>", table_contents, original_page)

# Now you can read the table with rvest
read_html(new_page)   %>%
  html_nodes("table") %>%
  html_table()

这是你想要的桌子。唯一的问题是,所有非ascii字符都显示为unicode,如u00de。您需要将这些内容下放到它们对应的字符中。

您的url指向一个包含空表的html页面。在web浏览器中可以看到表格内容的原因是html指示浏览器从其他页面下载表格内容并将其插入空表格中。当然,只读取第一页的html,而不运行javascript加载表数据

在您的例子中,数据是从指向JSON文件的另一个url加载的。实际上,可以将其内容插入原始html并使用rvest获取表。这实际上是手动执行浏览器的操作

require(httr)
require(magrittr)
require(rvest)

# Get the page's html as text
url <- 'https://novostavby.com/cs/developery/'
original_page <- GET(url) %>% content("text") 

# Get the JSON as plain text from the link generated by Javascript on the page
json_url <- "https://novostavby.com/ajax-estatio-developers/?citypath=undefined&sortdir=asc&sortfield=title&search=&pagefrom=developers"
JSON <- GET(json_url) %>% content("text", encoding = "utf8") 

# Remove the double escapes and enclosing brackets / html key from the JSON
# to get its html contents
table_contents <- JSON     %>%
 {gsub("\\\\n", "\n", .)}  %>%
 {gsub("\\\\/", "/", .)}   %>%
 {gsub("\\\\\"", "\"", .)} %>%
  strsplit("html\":\"")    %>%
  unlist                   %>%
  extract(2)               %>%
  substr(1, nchar(.) -2)   %>% 
  paste0("</tbody>")

# insert the table html into the original page
new_page <- gsub("</tbody>", table_contents, original_page)

# Now you can read the table with rvest
read_html(new_page)   %>%
  html_nodes("table") %>%
  html_table()

这是你想要的桌子。唯一的问题是,所有非ascii字符都显示为unicode,如u00de。您需要将这些数据复制到它们的等效字符。

表中除标题之外的行将从服务器或其他数据源动态加载。像这样删除源代码只会得到页面中的静态内容。您可以尝试使用Selenium之类的工具从服务器或其他数据源动态加载表中除标题之外的行。像这样删除源代码只会得到页面中的静态内容。你可以尝试像selenium这样的工具