How to Build a Python Bot that Emails Random Song Lyrics Daily
This tutorial shows how to use Python to scrape song lyrics from a music website, format them, and automatically email them each day, with optional scheduling via Windows Task Scheduler, providing full source code and step‑by‑step explanations.
Introduction
Hello, I’m a Python enthusiast. In a recent Python group a senior member shared an interesting script that automatically sends song lyrics to an email address on a schedule. I’m sharing the complete solution here.
Implementation Idea
The approach consists of three parts: (1) a Python web‑scraper that fetches lyric data from a public music API, (2) an email‑sending routine that builds an SMTP message with the lyrics, and (3) a scheduler—Windows Task Scheduler—to run the script periodically.
Implementation Process
The core code is shown below. It retrieves a random song ID, extracts the song name and lyrics, and sends them via a QQ mail SMTP server.
import json, random
import requests
import parsel
import smtplib
import schedule
import time
from bs4 import BeautifulSoup
from email.mime.text import MIMEText
from email.header import Header
def recipe_spider():
headers = {
'Cookie': 'kw_token=6A3S4588YMS',
'csrf': '6A3S4588YMS'
}
url = 'http://www.kuwo.cn/api/www/bang/bang/musicList?bangId=93&pn=1&rn=30'
resp = requests.get(url, headers=headers)
text = resp.text
text_dict = json.loads(text)
musicList = text_dict['data']['musicList']
music_list = []
for music in musicList:
dit = {}
dit['musicrid'] = music['musicrid'].split('_')[1]
dit['name'] = music['name']
music_list.append(dit.copy())
list_num = []
for i in range(30):
music_num = music_list[i]['musicrid']
list_num.append(music_num)
a = random.choice(list_num)
url1 = 'http://www.kuwo.cn/play_detail/' + a
html_data = requests.get(url=url1).text
sel = parsel.Selector(html_data)
name = sel.xpath('//*[@class="song_name flex_c"]/span/text()').get().strip()
lyric = sel.xpath('//*[@id="lyric"]/div/p/text()').getall()
lyric1 = '
'.join(lyric)
return lyric1
def send_email(lyric1):
global account, password, receiver
mailhost = 'smtp.qq.com'
qqmail = smtplib.SMTP_SSL(mailhost, 465)
qqmail.login(account, password)
content = 'Dear, today the song is:
' + lyric1
message = MIMEText(content, 'plain', 'utf-8')
subject = 'What to listen today (with lyrics)'
message['Subject'] = Header(subject, 'utf-8')
try:
qqmail.sendmail(account, receiver, message.as_string())
print('Email sent successfully')
except:
print('Email sending failed')
qqmail.quit()
def job():
print('Starting a task')
lyric1 = recipe_spider()
send_email(lyric1)
print('Task completed')
if __name__ == '__main__':
job()Running the Script
After filling in your email address, the corresponding authorization code, and the recipient’s address, run the script. An email containing the song name and its lyrics will be delivered to the specified inbox.
Scheduling with Windows Task Scheduler
You can automate the script to run daily using Windows Task Scheduler. Create a new task, set the trigger time, and point the action to the Python interpreter with this script as the argument.
Conclusion
This article demonstrates a small project that combines Python web scraping, email automation, and Windows scheduling to deliver random song lyrics to your inbox each day, offering a practical example of backend automation.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
