简体   繁体   中英

How to read in whole message not just snippet

 msg = service.users().messages().get(userId='me', id=message['id']).execute()
 print(msg['snippet']) 

I am currently using the above code, which doesn't get the whole message. I have seen in the documentation that the google API has raw and full options, but the raw option doesn't print in a readable way and I cannot get the full option to work.

Thank you !

This is how worked for me:

# Gets message header first
msg_header = service.users().messages().get(
                                                    userId=user_id,
                                                    id=msg_id,
                                                    format="full",
                                                    metadataHeaders=None
                                                    ).execute()

# Gets message body from header
body = base64.urlsafe_b64decode(msg_header.get("payload").get("body")\
            .get("data").encode("ASCII")).decode("utf-8")

The body comes in HTML so, in my case, I use BeautifulSoup to extract the information I need, like below:

soup = bs(body, 'html.parser')

# Loop on e-mail table
for row in soup.findAll('tr'): 
            aux = row.findAll('td')
            info[aux[0].string] = aux[1].string

The information extraction will depend on the pattern of the message. In my case, all messages that I'm getting have the same pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM