首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用GmailR提取所有电子邮件

使用GmailR提取所有电子邮件
EN

Stack Overflow用户
提问于 2020-06-17 10:51:44
回答 1查看 109关注 0票数 0

我正在尝试从我的gmail账户中提取所有的电子邮件来做一些分析。最终目标是电子邮件的数据帧。我正在使用gmailR包。

到目前为止,我已经提取了所有电子邮件线程,并通过将所有线程ID映射到gm_thread()来“扩展”它们。下面是实现该功能的代码:

代码语言:javascript
复制
threads <- gm_threads(num_results = 5)

thread_ids <- gm_id(threads)
#extract all the thread ids

threads_expanded <- map(thread_ids, gm_thread)

这将返回所有线程的列表。它的结构是gmail_thread对象的列表。当您向下钻取线程对象列表str(threads_expanded[[1]], max.level = 1)的一个级别时,您会得到一个线程对象,如下所示:

代码语言:javascript
复制
List of 3
 $ id       : chr "xxxx"
 $ historyId: chr "yyyy"
 $ messages :List of 3
 - attr(*, "class")= chr "gmail_thread"

然后,如果你进一步深入到组成线程的消息中,你就会开始获得有用的信息。str(threads_expanded[[1]]$messages, max.level = 1)为您提供了该线程的gmail_message对象列表:

代码语言:javascript
复制
List of 3
 $ :List of 8
  ..- attr(*, "class")= chr "gmail_message"
 $ :List of 8
  ..- attr(*, "class")= chr "gmail_message"
 $ :List of 8
  ..- attr(*, "class")= chr "gmail_message"

我被卡住的地方实际上是从所有线程中的每个电子邮件中提取所有有用的信息。最终目标是一个包含message_id、thread_id、to、from等列的数据帧。

代码语言:javascript
复制
    message_id    |  thread_id    |  to            |  from            | ... |
    -------------------------------------------------------------------------
    1234          |  abcd         |  me@gmail.com  | pam@gmail.com    | ... |
    1235          |  abcd         |  pam@gmail.com | me@gmail.com     | ... |
    1236          |  abcf         |  me@gmail.com  | tim@gmail.com    | ... |
EN

回答 1

Stack Overflow用户

发布于 2020-06-30 09:55:38

这不是最漂亮的答案,但它是有效的。我将在稍后对其进行矢量化:

代码语言:javascript
复制
threads <- gm_threads(num_results = 5)

thread_ids <- gm_id(threads)
#extract all the thread ids

threads_expanded <- map(thread_ids, gm_thread)

msgs <- vector()
for(i in (1:length(threads_expanded))){
  msgs <- append(msgs, values = threads_expanded[[i]]$messages)
}
#extract all the individual messages from each thread

msg_ids <- unlist(map(msgs, gm_id))
#get the message id for each message

msg_body <- vector()
#get message body, store in vector
for(msg in msgs){
  body <- gm_body(msg)
  attchmnt <- nrow(gm_attachments(msg))
  if(length(body) != 0 && attchmnt == 0){
    #does not return a null value, rather an empty list or list
of length 0, so if,
    #body is not 0 (there is something there) and there are no attachemts, 
    #add it to vector
    msg_body <- append(msg_body, body)
    #if there is no to info, fill that spot with an empty space
  }
  else{
    msg_body <- append(msg_body, "")
    #if there is no attachment but the body is also empty add "" to the list
  }
}
msg_body <- unlist(msg_body)

msg_datetime <- msgs %>%
  map(gm_date) %>%
  unlist()%>%
  dmy_hms()
#get datetime info, store in vector

message_df <- tibble(msg_ids, msg_datetime, msg_body)
#all the other possible categories, e.g., to, from, cc, subject, etc.,
#either use a similar for loop or a map call
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62420703

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档