Python Forum
find a hyperlink in Gmail body python 3(imap and selenium) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: find a hyperlink in Gmail body python 3(imap and selenium) (/thread-16764.html)



find a hyperlink in Gmail body python 3(imap and selenium) - taomihiranga - Mar-13-2019

I am making a program to automatically read emails and click automatically a hyperlink in that email body.

A taxi company provides contractions from email. immediately need to click on link in email.

I am using imaplib , selenium , re to code this. I successfully coded the login to email using imaplib and read mail.

Now i need to get hyperlink from mail.(hyperlink text is confirm)
i used a code for getting hyperlink but hyperlink is wrong.

show URL with more other characters.....url not working...
if have more hyperlinks how get a one hyperlink. can get a hyperlink using link text?


code given below
import imaplib 
import email

mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('[email protected]', 'xxxxxxxx')
mail.list()
# Out: list of "folders" aka labels in gmail.
mail.select("inbox") # connect to inbox.

result, data = mail.search(None, "ALL")

ids = data[0] # data is a list.
id_list = ids.split() # ids is a space separated string
latest_email_id = id_list[-1] # get the latest

result, data = mail.fetch(latest_email_id, "(RFC822)") # fetch the email body (RFC822) for the given ID

raw_email = data[0][1] # here's the body, which is raw text of the whole email
# including headers and alternate payloads
raw_email = str(raw_email)
import re
print(re.search("(?P<url>https?://[^\s]+)", raw_email).group("url"))



RE: find a hyperlink in Gmail body python 3(imap and selenium) - Gamer1057 - Dec-30-2020

Did you ever get this working?