3

我每天都会收到一封电子邮件,在该电子邮件中,有一个下载链接。当您单击下载链接时,每次都会以不同的文件名下载一个 csv 文件。最重要的是,下载链接名称每天都会更改。

有没有办法在作为超链接的电子邮件正文中调用文本,并将下载的文件读取到 R?

我的电子邮件如下所示:

在此处输入图像描述

我从电子邮件中读取任何内容的常用代码如下所示:

##Load Libraries
library(readr)
library(RDCOMClient)
library(plotrix)
outlook_app <- COMCreate("Outlook.Application")
search <- outlook_app$AdvancedSearch(
  "Inbox",
  "urn:schemas:httpmail:subject = 'SUBJECT NAME'"
)

Sys.sleep(5) # Wait a hot sec!


results <- search$Results() # Saves search results into results object

Sys.sleep(5) # Wait a hot sec!

results$Item(1)$ReceivedTime() # Received time of first search result

as.Date("1899-12-30") + floor(results$Item(1)$ReceivedTime()) # Received date

# Iterates through results object to pull out all of the items
for (i in 1:results$Count()) {
  if (as.Date("1899-12-30") + floor(results$Item(i)$ReceivedTime()) 
      == as.Date(Sys.Date())) {
    email <- results$Item(i)
  }
}

attachment_file <- tempfile()
email$Attachments(1)$SaveAsFile(attachment_file)

##Automatically Determine csv file name
file_name<-unzip(attachment_file,list=TRUE)
csv_file<-file_name$Name

##Read CSV File
df <- read_csv(unz(attachment_file,csv_file), skip = 25)
4

0 回答 0