Python program to extract email id from url text file

File handling | Python Methods and Functions | Regular Expressions

Example:

  Input:  Hello This is Geeksforgeeks [email protected] [email protected] GfG is a portal for geeks [email protected] careers @ python.engineering  Output:  [] [] ['review-team @ python.engineering'] [' contribute @ python.engineering'] [] ['feedback @ python.engineering'] [' careers @ python.engineering'] 

URL text file can be parsed with urllib.request . To extract emails using regular expressions, the re library can be used. For more information on regular expressions, refer to

# library that processes URL content

import urllib.request

 
# Module import is required for
# regular expressions

import re

 
# Assign urlopen to file object variable

fhand = urllib.request.urlopen

  ( ' https://media.python.engineering/wp-content/uploads/e-mail-1.txt ' )

 

for line in fhand:

# Get a text file

# content line by line.

s = line.decode (). strip ()

 

  # regular expression to extract all addresses in email

# from text file

reg = re.findall (r "[A-Za-z0-9 ._% + -] +"

  r "@ [A-Za-z0-9 .-] +"

r ". [A-Za-z] {2,4} " , s)

  

# print output list

print (reg)

Output:

 [] [] ['review-team @ python.engineering'] [' contribute @ python.engineering'] [] ['feedback @ python.engineering'] [' careers @ python.engineering'] 




Get Solution for free from DataCamp guru