Posts: 8
Threads: 3
Joined: Feb 2021
Hi,
I'm having a bit of trouble getting information into an automatic email that I'm having python send for me.
To start I have selenium webscrape this site for job posts then package that into a list format. From there I want to call a function that sends an email to my personal Gmail with the list of web scraped data as the input to that function.
Currently, the output of the list is like this: ['Software Engineer and Technical Writer – Winter/Spring/Summer Intern\nTethers Unlimited, Inc. Connect the Universe', 'Sr. Embedded Software Engineer III\nTethers Unlimited, Inc. Connect the Universe', 'Sr Product Manager – Technical – Software Defined Radios\nTethers Unlimited, Inc. Connect the Universe', 'Internships\nTethers Unlimited, Inc. Connect the Universe']
Ideally, I'd also love some help formatting the list so that when it went into the email function it was separated by a new row at each new title for easier differentiation.
I'll attach the main code and function code below so that people can take a look.
Thanks in advance for any help!
CK
Posts: 8
Threads: 3
Joined: Feb 2021
Feb-17-2021, 03:54 AM
(This post was last modified: Feb-17-2021, 09:37 AM by buran.)
Main Code:
import time
from selenium import webdriver
from send_email import send_email
# Chromes web driver is installed on your PATH so it can be accessed like this without pathing.
url = "http://www.tethers.com/careers/"
driver = webdriver.Chrome()
driver.get(url)
time.sleep(5)
title_list = driver.find_elements_by_class_name('position')
job_list = []
for title in title_list:
job_list.append(title.text)
send_email(job_list) Function code:
def send_email(jobs):
import smtplib, ssl
port = 465
password = input("Type your password and press enter: ")
sender_email = "[email protected]"
receiver_email = "[email protected]"
message = """\
Subject: Tethers Unlimited Jobs
Here are the new jobs from Tethers Unlimited: {job}"""
print(message)
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
server.login("[email protected]", password)
server.sendmail(sender_email, receiver_email, message.format(job=jobs))
buran write Feb-17-2021, 09:37 AM:Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Posts: 8,155
Threads: 160
Joined: Sep 2016
def send_email(jobs):
import smtplib, ssl
port = 465
password = input("Type your password and press enter: ")
sender_email = "[email protected]"
receiver_email = "[email protected]"
message = """\
Subject: Tethers Unlimited Jobs
Here are the new jobs from Tethers Unlimited: {job}"""
print(message)
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
server.login("[email protected]", password)
server.sendmail(sender_email, receiver_email, message.format(job='\n'.join(jobs))) or even better - use f-strings
def send_email(jobs):
import smtplib, ssl
port = 465
password = input("Type your password and press enter: ")
sender_email = "[email protected]"
receiver_email = "[email protected]"
message = f"""\
Subject: Tethers Unlimited Jobs
Here are the new jobs from Tethers Unlimited: {'\n'.join(jobs)}"""
print(message)
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
server.login("[email protected]", password)
server.sendmail(sender_email, receiver_email, message)
Posts: 8
Threads: 3
Joined: Feb 2021
Feb-18-2021, 07:45 AM
(This post was last modified: Feb-18-2021, 08:58 AM by buran.)
(Feb-17-2021, 09:39 AM)buran Wrote: def send_email(jobs):
import smtplib, ssl
port = 465
password = input("Type your password and press enter: ")
sender_email = "[email protected]"
receiver_email = "[email protected]"
message = """\
Subject: Tethers Unlimited Jobs
Here are the new jobs from Tethers Unlimited: {job}"""
print(message)
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
server.login("[email protected]", password)
server.sendmail(sender_email, receiver_email, message.format(job='\n'.join(jobs))) or even better - use f-strings
def send_email(jobs):
import smtplib, ssl
port = 465
password = input("Type your password and press enter: ")
sender_email = "[email protected]"
receiver_email = "[email protected]"
message = f"""\
Subject: Tethers Unlimited Jobs
Here are the new jobs from Tethers Unlimited: {'\n'.join(jobs)}"""
print(message)
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
server.login("[email protected]", password)
server.sendmail(sender_email, receiver_email, message)
Hi Buran,
Thanks for the help. I tried the f-string method and that worked well. For some reason, the f-string wasn't liking the '\n' within the curly brackets of the message variable, it kept spitting out this error:
Error: "SyntaxError: f-string expression part cannot include a backslash"
So I just created a linebreak variable and gave it a value of ='\n' that seemed to fix the error.
Everything works great until the function returns I believe. Then I get this error:
Error: Traceback (most recent call last):
File "C:/Users/Cknut/venv/PersonalCrawlers/TethersUnlimited.py", line 17, in <module>
send_email(job_list)
File "C:\Users\Cknut\venv\PersonalCrawlers\send_email.py", line 20, in send_email
server.sendmail(sender_email, receiver_email, message)
File "C:\Users\Cknut\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 859, in sendmail
msg = _fix_eols(msg).encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode character '\u2013' in position 134: ordinal not in range(128)
Can you tell me why the below code fixes this issue. I added .encode('utf-8').strip to the end of message and it seems to fix the issue. Is this just because I'm taking out the formatting that is built into this python version? Thanks!
Here is the function currently:
def send_email(jobs):
import smtplib, ssl
port = 465
password = input("Type your password and press enter: ")
sender_email = "[email protected]"
receiver_email = "[email protected]"
linebreak = '\n'
message = f"""\
Subject: Tethers Unlimited Jobs
Here are the new jobs from Tethers Unlimited: {linebreak.join(jobs)}"""
print(message)
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
server.login("[email protected]", password)
server.sendmail(sender_email, receiver_email, message.encode('utf-8').strip())
Posts: 8,155
Threads: 160
Joined: Sep 2016
Feb-18-2021, 09:13 AM
(This post was last modified: Feb-18-2021, 09:13 AM by buran.)
Your msg contains non-ascii chars.
As stated in the docs for sendmail():
Quote:msg may be a string containing characters in the ASCII range, or a byte string. A string is encoded to bytes using the ascii codec, and lone \r and \n characters are converted to \r\n characters. A byte string is not modified.
When you pass str it try to encode it with ascii codec. If you encode it yourself (with utf-8 ) you pass byte string and thus no error.
Sorry, about the backslash in the f-string. It slipped from my mind. More clean code would be
jobs = '\n'.join(jobs)
message = f"""
Subject: Tethers Unlimited Jobs
Here are the new jobs from Tethers Unlimited: {jobs}""" Also, note that no need of line continuation char. triple quotes are meant to be multi-line.
|