Skip to content

Instantly share code, notes, and snippets.

@jamesshah
Last active October 1, 2019 13:57
Show Gist options
  • Save jamesshah/096c9bfaee8972162c1949441106db63 to your computer and use it in GitHub Desktop.
Save jamesshah/096c9bfaee8972162c1949441106db63 to your computer and use it in GitHub Desktop.
Random Quote Generator And Send Using Email | Python Web Scrapping Using BeautifulSoup
import requests, random
from bs4 import BeautifulSoup
import smtplib
#Function to generate a random quote.
def random_quote(url):
conn = requests.get(url)
soup = BeautifulSoup(conn.content,'html.parser')
quotes = soup.find_all('a', attrs={"title": "view quote"})
quotesList = []
for quote in quotes:
quotesList.append(quote.text)
num = random.randint(0,len(quotesList))
your_quote = 'Here is Your Quote:\n"'+quotesList[num]+'"'
return(print(your_quote))
#function to send generated quote using email
def send_email(mail):
sender = input("Enter Sender's Email ID:")
passwd = input("Enter Sender's Password:")
#using apps password is recommended.
#or you'll have to enable less secure apps in your google account.
reciever = input("Enter Reciever's Email ID:")
# If you want to use any other email service, you'll have to change arguements in below function.
# Refer smtplib documentation for more information.
server = smtplib.SMTP(host='smtp.gmail.com',port=587)
server.starttls()
server.ehlo()
server.login(sender,passwd)
server.sendmail(sender,reciever,mail)
print("Mail Sent Successfully!")
msg = random_quote('https://www.brainyquote.com/topics/inspirational-quotes')
mail = f'Subject: "Quote Of The Day"\n\n{msg}'
send_email(mail)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment