首页 > 解决方案 > 不能用 rvest 刮桌子

问题描述

我一直试图从这个页面刮一张桌子:https ://ficeda.com.mx/index.php?id=precios

我的代码看起来像这样

url_data <- "https://ficeda.com.mx/index.php?id=precios"

url_data %>% 
  read_html() %>% 
  html_node(xpath = "/html/body/div[1]/div/div/div[3]/table[1]") %>% 
  html_table()

但它给了我这个错误:UseMethod中的错误(“html_table”):没有适用于“html_table”的方法应用于“xml_missing”类的对象

有谁知道可能会发生什么?

谢谢!

标签: rweb-scrapingrvest

解决方案


该数据实际上位于 iframe 中。或者,发出您的初始请求并从 iframe 中提取 src 链接,然后在那里发出请求,或者直接向 iframe 文档发出请求:

library(rvest)

url_data <- "https://www.ficeda.com.mx/app_precios/pages/index/v2"

url_data %>% 
  read_html() %>% 
  html_node('.category_title + table') %>% 
  html_table()

来自原始端点的 iframe src:

read_html('https://ficeda.com.mx/index.php?id=precios') %>% html_node('#linea2') %>% html_attr('src')

推荐阅读