gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 06:35:21 26 4
gpt4 key购买 nike

我每天都会收到一封电子邮件,在该电子邮件中,有一个下载链接。当您单击下载链接时,每次都会下载一个不同文件名的 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)

最佳答案

你可以考虑这样的事情:

library(RDCOMClient)
library(stringr)

## create outlook object
OutApp <- COMCreate("Outlook.Application")
outlookNameSpace <- OutApp$GetNameSpace("MAPI")
fld <- outlookNameSpace$GetDefaultFolder(6)

# Check that we got the right folder
Cnt <- fld$Items()$Count()
emails <- fld$items
list_Text_Body <- list()
list_URL_Link <- list()

for(i in 1 : Cnt)
{
print(i)
list_Text_Body[[i]] <- emails(i)[["Body"]]
list_URL_Link[[i]] <- stringr::str_extract_all(list_Text_Body[[i]], pattern = "http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+")[[1]]
}

read.csv(list_URL_Link[[1]][[1]])

关于r - 从电子邮件下载链接并将该文件调用到 R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49693062/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com