首页 > 解决方案 > 从网站以纯文本/文本格式下载 CSV

问题描述

我正在尝试自动从网站下载数据集,但无法获得我想要的东西。我尝试过使用RCurl,但它遇到了tlsv1 alert protocol version错误。我可以使用 执行下载httr,但我收到的是普通/html 格式的文件,这显然不是我想要的。我已经尝试了一些其他的东西,但似乎没有任何效果。请指教。

下载代码httr

###lung cancer screening locator tool url
url1 = "https://report.acr.org/#/site/PUBLIC/views/NRDRLCSLocator/ADownload.csv"

GET(url1, write_disk(tf <- tempfile(fileext = ".csv"))) #produces file of content type 'plain/html'

lcsr = read.csv(tf)

此请求的原始网站是https://www.acr.org/Clinical-Resources/Lung-Cancer-Screening-Resources/LCS-Locator-Tool,其背后的 Tableau 位于https://report.acr。 org/t/PUBLIC/views/NRDRLCSLocator/LCSLocator?:embed=y&:showVizHome=no&:host_url=https%3A%2F%2Freport.acr.org%2F&:embed_code_version=3&:tabs=no&:toolbar=no&:showAppBanner =no&:display_spinner=no&:loadOrderID=0

标签: rhttrrcurl

解决方案


一个RSelenium解决方案,

按此设置下载目录,

library(RSelenium)

#Setting download directory, 
eCaps <- list(
  chromeOptions = 
    list(prefs = list('download.default_directory' = "D:\\mywork"))
)
driver <- rsDriver(browser = "chrome", extraCapabilities = eCaps)
remDr <- driver[["client"]]
remDr$navigate("https://report.acr.org/#/site/PUBLIC/views/NRDRLCSLocator/ADownload.csv")
library(readr)
df = read_csv('ADownload.csv')

推荐阅读