#!/usr/bin/env python # coding: utf-8 # In[17]: from bs4 import BeautifulSoup from bs4.element import Comment import urllib.request # In[102]: url = "https://www.spc.noaa.gov/products/fire_wx/fwdy1.html" def tag_visible(element): if element.parent.name in ['style', 'script', 'head', 'title', 'meta', '[document]']: return False if isinstance(element, Comment): return False return True def text_from_html(body): soup = BeautifulSoup(body, 'html.parser') texts = soup.findAll(text=True) visible_texts = filter(tag_visible, texts) return u" ".join(t.strip() for t in visible_texts) html = urllib.request.urlopen(url).read() wx = text_from_html(html) a = wx.splitlines() wxemail = [] for x in a: x = str(x) print(x) wxemail.append(x) # In[103]: def send_email(user, pwd, recipient, subject, body): import smtplib body.encode('ascii',errors='ignore') body.encode('utf-8',errors='replace') body=body.encode('ascii','ignore').decode('ascii') gmail_user = user gmail_pwd = pwd FROM = user TO = recipient if type(recipient) is list else [recipient] SUBJECT = subject TEXT = body # Prepare actual message message = """From: %s\nTo: %s\nSubject: %s\n\n%s """ % (FROM, ", ".join(TO), SUBJECT, TEXT) try: server = smtplib.SMTP("10.12.112.10", 25) #server = smtplib.SMTP("192.31.208.52",443) #server.ehlo() #server.starttls() #server.login(gmail_user, gmail_pwd) server.sendmail(FROM, TO, message) server.close() print('successfully sent the email') except Exception as ex: print("failed to send mail") print(ex) # In[ ]: send_email('fireline@verisk.com', '', ['kaitlyn.perham@verisk.com'], 'WildfireWeather',"\n".join(str(x) for x in wxemail))