【问题标题】:Finding all csv links from website using R使用 R 从网站中查找所有 csv 链接
【发布时间】:2021-03-12 17:06:56
【问题描述】:

我正在尝试从 ICE 网站 (https://www.theice.com/clear-us/risk-management#margin-rates) 下载包含保证金策略信息的数据文件。我试图通过在 R 中应用以下代码来做到这一点:

page <- read_html("https://www.theice.com/clear-us/risk-management#margin-rates")
raw_list <- page %>% # takes the page above for which we've read the html
html_nodes("a") %>%  # find all links in the page
html_attr("href") %>% # get the url for these links
str_subset("\\.csv") # find those that end in csv only

但是,它只能找到两个 csv 文件。也就是说,当点击 Margin Rates 并转到 Historic ICE Risk Model Parameter 时,它不会检测到任何显示的文件。见下文

raw_list
[1] "/publicdocs/iosco_reporting/haircut_history/icus/ICUS_Asset_Haircuts_History.csv"   
[2] "/publicdocs/iosco_reporting/haircut_history/icus/ICUS_Currency_Haircuts_History.csv"

我想知道如何才能做到这一点,所以稍后我可以选择文件并下载它们。

提前非常感谢

【问题讨论】:

    标签: r csv download


    【解决方案1】:

    我们可以在浏览器 devtools 中查看网络流量,以找到每个下拉操作的 url。

    Historic ICE Risk Model Parameter 下拉列表来自该页面: https://www.theice.com/marginrates/ClearUSMarginParameterFiles.shtml;jsessionid=7945F3FE58331C88218978363BA8963C?getParameterFileTable&category=Historical

    我们删除 jsessionid(根据 QHarr 的评论)并将其用作我们的端点:

    endpoint <- "https://www.theice.com/marginrates/ClearUSMarginParameterFiles.shtml?getParameterFileTable&category=Historical"
    page <- read_html(endpoint)
    

    然后我们可以得到完整的csv列表:

    raw_list <- page %>%
        html_nodes(".table-partitioned a") %>% # add specificity as QHarr suggests
        html_attr("href")
    

    输出:

    '/publicdocs/clear_us/irmParameters/ICUS_MARGIN_INTERMONTH_20210310.CSV'
    '/publicdocs/clear_us/irmParameters/ICUS_MARGIN_INTERCONTRACT_20210310.CSV'
    '/publicdocs/clear_us/irmParameters/ICUS_MARGIN_SCANNING_20210310.CSV'
    '/publicdocs/clear_us/irmParameters/ICUS_MARGIN_STRATEGY_20210310.CSV'
    '/publicdocs/clear_us/irmParameters/ICUS_MARGIN_INTERMONTH_20210226.CSV'
    '/publicdocs/clear_us/irmParameters/ICUS_MARGIN_INTERCONTRACT_20210226.CSV'
    '/publicdocs/clear_us/irmParameters/ICUS_MARGIN_SCANNING_20210226.CSV'
    '/publicdocs/clear_us/irmParameters/ICUS_MARGIN_STRATEGY_20210226.CSV'
    ...
    

    【讨论】:

      【解决方案2】:

      页面似乎没有立即加载页面的该部分,并且您的请求中缺少该部分。网络监视器指示文件“ClearUSRiskArrayFiles.shtml”在 400 毫秒后被加载。一旦您在 URL 中指定年份和月份,该文件似乎会提供所需的链接。

      library(rvest)
      library(stringr)
      
      page <- read_html("https://www.theice.com/iceriskmodel/ClearUSRiskArrayFiles.shtml?getRiskArrayTable=&type=icus&year=2021&month=03")
      
      raw_list <- page %>% # takes the page above for which we've read the html
        html_nodes("a") %>%  # find all links in the page
        html_attr("href")
      
      head(raw_list[grepl("csv", raw_list)], 3L)
      #> [1] "/publicdocs/irm_files/icus/2021/03/NYB0312E.csv.zip"
      #> [2] "/publicdocs/irm_files/icus/2021/03/NYB0311E.csv.zip"
      #> [3] "/publicdocs/irm_files/icus/2021/03/NYB0311F.csv.zip"
      

      reprex package (v1.0.0) 于 2021-03-12 创建

      【讨论】:

      • 感谢您的回复 Jan。我试过这个,但我一直只阅读上面显示的两个文件。见这里raw_list &lt;- page %&gt;% # takes the page above for which we've read the html + html_nodes("a") %&gt;% # find all links in the page + html_attr("href") %&gt;% # get the url for these links + str_subset("\\.[cC][sS][vV]$") &gt; raw_list [1] "/publicdocs/iosco_reporting/haircut_history/icus/ICUS_Asset_Haircuts_History.csv" [2] "/publicdocs/iosco_reporting/haircut_history/icus/ICUS_Currency_Haircuts_History.csv"
      • 你是对的。那是另外一回事。我找到了找到丢失文件的方法。但是,您不会一次性获得所有文件。
      • 没问题。感谢您的时间一月
      猜你喜欢
      • 1970-01-01
      • 2013-10-04
      • 2010-11-29
      • 1970-01-01
      • 2023-03-03
      • 2021-08-19
      • 2017-02-24
      • 1970-01-01
      • 2019-02-22
      相关资源
      最近更新 更多