HTML 包含导致问题的格式错误的标记,具体而言<spoiler:3tbt4d3m>
,正如错误所暗示的那样。如果您使用 httr 获取 HTML 而不对其进行解析,则可以使用正则表达式来删除该标记及其内容而不会发生意外,因为快速查看会发现它不包含该表。
library(httr)
library(rvest)
url <- 'http://spacefem.com/pregnant/due.php?use=EDD&m=09&d=10&y=16'
html <- url %>% GET(user_agent('R')) %>% content('text')
html2 <- gsub('<spoiler:3tbt4d3m>.*</spoiler:3tbt4d3m>', '', html)
df <- html2 %>% read_html() %>%
html_node(xpath = '//table[@border="1"]') %>%
# obviously insufficient to parse double headers, but at least the data exists now
html_table(fill = TRUE)
df[1:5, 1:3]
## Date Progress Overall probability ofspontaneous labor
## 1 Date Progress On this date
## 2 Saturday August 6th, 2016 35W, 0D 0.01%
## 3 Sunday August 7th, 2016 35W, 1D 0.01%
## 4 Monday August 8th, 2016 35W, 2D 0.02%
## 5 Tuesday August 9th, 2016 35W, 3D 0.02%
混合正则表达式和 HTML 让我有点不安,所以也许有一种更清洁的整理方式,但在解析之前我不确定它会是什么。