从电子邮件下载链接并将该文件调用R

时间:2018-04-06 12:41:45

标签: r string outlook rvest rdcomclient

我每天都有一封电子邮件发送给我,在该电子邮件中,有一个下载链接。当您单击下载链接时,每次都会使用不同的文件名下载一个csv文件。除此之外,下载链接名称每天都会更改。

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

我的电子邮件如下:

enter image description here

我通常用电子邮件阅读任何内容的代码如下:

##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)

0 个答案:

没有答案