首页 > 解决方案 > Web 在 R 中抓取 HTML 表格需要大量时间

问题描述

伙计们,我正在尝试抓取一个只有大约 1000 多条记录的链接,但要花几个小时才能得到它们。想知道我是否做错了什么或将其加载到表中的方法。

urlString = "https://www.valueresearchonline.com/funds/selector-data/primary-category/1/equity/?tab=snapshot&output=html-data"
urlString <- URLencode(paste0(urlString,""))

#Reading the HTML code from the website and process the text
getHTML <- xml2::read_html(urlString, options = "HUGE")

#This one keeps running endlessly and doesn't load the table
mytable <- data.frame(getHTML %>% html_table(fill = T, trim = T))

任何帮助,将不胜感激。谢谢

标签: rweb-scrapingrvestxml2

解决方案


该链接是一个 JSON 文件。您需要先阅读它jsonlite。HTML 数据位于html_data节点上,您可以通过以下方式读取此节点read_html

json <- jsonlite::fromJSON("https://www.valueresearchonline.com/funds/selector-data/primary-category/1/equity/?tab=snapshot&output=html-data")
getHTML <- xml2::read_html(json$html_data)
mytable <- data.frame(getHTML %>% html_table(fill = T, trim = T))

推荐阅读