Skip to main content
Practice

Sending Data Scraped from Wikipedia via Email

In this session, we'll learn how to send a CSV file of date information gathered from web crawling historical events as an email attachment.


1. Import Required Libraries

  • email, smtplib: Libraries for creating and sending emails.

  • io: Handles data processing using the computer's memory.


2. Save CSV File in Memory

Save CSV File in Computer Memory
# Save as CSV format in memory
csv_buffer = io.StringIO()

# Save as CSV file
df[['Historical Event', 'Date']].to_csv(csv_buffer, index=False)

# Reset the memory pointer to the start
csv_buffer.seek(0)

Using the io.StringIO object, we store data in memory and then convert it to a CSV file.

This CSV file contains information on Historical Events and Dates.


3. Compose and Send Email

Compose and Send Email
# Create email body
def create_email_body():
return """
<html>
<body>
<h1>Historical Events Date Information</h1>
<p>Hello, CodeFriends here.</p>
<p>Please check the historical events and date information in the attached CSV file.</p>
</body>
</html>
"""

# Function to send email
def send_email(to_email, subject, body, attachment_data, attachment_name):
msg = MIMEMultipart()
msg['Subject'] = subject
...(truncated)...

# Send email
send_email(receiver_email, "[CodeFriends] Historical Events Date Information", create_email_body(), csv_buffer, 'historical_events_dates.csv')

Compose the email body and attach the CSV file, then send the email using smtplib.


By combining web crawling with email sending, you can create a seamless workflow to collect and deliver data. Customize the sample code and Excel file to suit your needs, and create a practical program you can apply immediately in your job. 🙂

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.