DEV Community

Pandeyashish17
Pandeyashish17

Posted on

Lets build an email scraper in python that will scrape a bunch of emails from text and save them in a .csv file

Welcome to the Email Extractor-inator! This wacky Python program will turn your messy pile of emails into a neatly organized CSV file. Just follow these simple steps:

  1. Copy and paste your string of email-filled gibberish into the input_string variable.

  2. Sit back and watch as the Email Extractor-inator uses its trusty regex (regular expression) to pluck out all the emails like a chicken at a county fair.

  3. The extracted emails will be stored in the emails list, ready to be written into a CSV file with a simple command.

  4. BAM! Your emails are now organized and easy to access. No more digging through piles of paper or scrolling through a never-ending inbox.

Are you ready to try out the Email Extractor-inator? Here's the code:

import csv
import re

# input string containing emails
input_string = '''
Paste your string of emails here.
'''

# regex to extract emails
regex = r'[\w\.-]+@[\w\.-]+'

# extract all emails
emails = re.findall(regex, input_string)

# write extracted emails to a CSV file
with open('emails.csv', 'w') as csvfile:
    writer = csv.writer(csvfile)
    for email in emails:
        writer.writerow([email])

Enter fullscreen mode Exit fullscreen mode

Give the Email Extractor-inator a try and see how it can revolutionize your email organization game!

If you don't know what to search for to scrape emails, click here. It will redirect you to a Google search with a pre-filled query. You can modify the query to fit your needs. just ctrl + A and ctrl + c and paste in the input_string variable

Top comments (1)

Collapse
 
joesuarez242 profile image
JoeSuarez242 • Edited

You made this process look so easy. However, note that when replacing the placeholder text "Paste your string of emails here." in the input_string variable with the actual string of email-filled gibberish that you want to extract and organize into a CSV file.

Once you have replaced the input_string with your actual data, you can run the code in a Python environment to extract the emails using the provided regular expression (regex) and store them in the emails list. The emails will then be written into a CSV file named "emails.csv" using the csv module.

After running the code, you can open the "emails.csv" file to view the neatly organized emails extracted from the input_string. You can further modify the code or CSV file as per your requirements for email organization. Happy email organizing with the Email Extractor Linkedin Navigator Extension!