【问题标题】:How to read a single file web page / web archive excel file into R如何将单个文件网页/网络存档excel文件读入R
【发布时间】:2019-04-08 01:11:34
【问题描述】:

是否可以将这个文件读入 R,而不必自己手动打开文件并重新保存为 excel 文件。下面显示这个文件实际上不是一个excel文件。

https://www.sec.gov/Archives/edgar/data/320193/000119312511192493/Financial_Report.xls

url <- "https://www.sec.gov/Archives/edgar/data/320193/000119312511192493/Financial_Report.xls"
temp_xls <- tempfile(fileext = ".xls")
download.file(url, destfile = temp_xls, mode = "wb")

readxl::format_from_signature(temp_xls)
#> [1] NA

readr::read_lines(temp_xls, n_max = 5)
#> [1] "MIME-Version: 1.0"                                                                                                                                                                                                                                               
#> [2] "X-Document-Type: Workbook"                                                                                                                                                                                                                                       
#> [3] "Content-Type: multipart/related; boundary=\"----=_NextPart_e3144909_1d60_4840_908e_3419ae0a14d3\""                                                                                                                                                               
#> [4] ""                                                                                                                                                                                                                                                                
#> [5] "This document is a Single File Web Page, also known as a Web Archive file.  If you are seeing this message, your browser or editor doesn't support Web Archive files.  Please download a browser that supports Web Archive, such as Microsoft Internet Explorer."


unlink(temp_xls)

1) 目前有没有办法将此文件读入 R

2) 是否可以自动手动打开文件并将其保存为 R 中可读的格式

【问题讨论】:

    标签: r


    【解决方案1】:

    一个可能的解决方案是使用httr:

    library(httr)
    library(XML)
    library(magrittr)
    h <- GET("https://www.sec.gov/Archives/edgar/data/320193/000119312511192493/Financial_Report.xls") %>% 
        content("text", encoding="UTF8") %>% 
        readHTMLTable()   
    

    编辑:添加一个使用工作表名称作为表格名称的版本

    library(httr)
    library(XML)
    library(xml2)
    library(magrittr)
    ctnt <- GET("https://www.sec.gov/Archives/edgar/data/320193/000119312511192493/Financial_Report.xls") %>% 
        content("text", encoding="UTF8") 
    tbls <- readHTMLTable(ctnt)   
    tbls <- tbls[names(tbls)!="NULL"]
    names(tbls) <- read_html(gsub("<!--[if gte mso 9]>", "", ctnt, fixed=TRUE)) %>% 
        xml_find_all(".//name") %>% 
        xml_text()
    

    【讨论】:

    • 这是一个很好的解决方案!我想知道是否有办法将选项卡名称分配给列表中的名称。
    • 添加了一些东西来设置名称,但你需要仔细检查
    猜你喜欢
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 2022-06-23
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多