Let’s Make A Bot That Applies To Jobs For Us With Python

I have had quite a lot of free time this so I decided to make some bots with python and selenium. This bot applies to jobs on craigslist automatically based on what you search. It goes to each posting in a specific query and sends cover letter complete with resume attachment.

Let’s get started.

First you will need to download and a install python 3.5 if you don’t already have it  you can download it here.

Now we have python installed on our computers let’s install our dependencies for this bot we will need to install selenium. Python installations should come with pip so you can install selenium by typing “pip install selenium”.

pip install selenium
We can start writing our script now. Lets begin by importing all the dependencies we will use.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import urllib.parse as urlparse
import time
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP
import smtplib

For selenium to work we need to download a chromedriver which we can get from here. Put the exe from the zip file in the same folder as script we are writing now.

Ok now that we imported our dependencies lets write a function to find the jobs that we are interested in. We’ll call it getJobs().

Our function will take a total of 5 parameters. We will have one for the job query, the cover letter, the resume file, and one for username and password to send emails.

def getJobs(q, cover, res, un, pw):

Now let’s initialize our browser with selenium and open the craigslist job page of the city we want. For this we will use Buffalo because that’s my hometown.

def getJobs(q, un , pw, cover, res):
    browser = webdriver.Chrome('chromedriver.exe')#this opens the browser
    browser.get("http://buffalo.craigslist.org/search/jjj")#tells the browser which page to go to

Now that we have our browser open and on the page we want next we have to fill out the search form so we can get the jobs we want. Selenium let’s us easily target elements on a page, fill out forms, and it’ll even let you simulate keystrokes and clicks.

def getJobs(q, un , pw, cover, res):
    browser = webdriver.Chrome('chromedriver.exe')#this opens the browser
    browser.get("http://buffalo.craigslist.org/search/jjj")#tells the browser which page to go to
    elem = browser.find_element_by_name("query")#we get the element by name
    elem.clear()
    elem.send_keys(q)#fills out form with our query
    elem.send_keys(Keys.RETURN)#simulates pressing the enter key

This should have us on the page we want with the jobs we were looking for. Now we need to make a list of all the urls for each job listing.

def getJobs(q, un , pw, cover, res):
    browser = webdriver.Chrome('chromedriver.exe')#this opens the browser
    browser.get("http://buffalo.craigslist.org/search/jjj")#tells the browser which page to go to
    elem = browser.find_element_by_name("query")#we get the element by name
    elem.clear()
    elem.send_keys(q)#fills out form with our query
    elem.send_keys(Keys.RETURN)#simulates pressing the enter key
    links = browser.find_elements(By.CSS_SELECTOR, '.result-title.hdrlnk')#this will return a list of elements using css selectors
    jobLinks = []

    for link in links:
        #We take the job title and url and add it to a list so we have a list of lists
        jobLinks.append([link.text,link.get_attribute("href")])

Ok now we have our list of jobs and a url for each one. Let’s tell the browser to go to each job page and get the contact information so we can send a email to the employer. We can do that by looping through the new list we just made jobLinks.

def getJobs(q, un , pw, cover, res):
    browser = webdriver.Chrome('chromedriver.exe') #this opens the browser
    browser.get("http://buffalo.craigslist.org/search/jjj") #tells the browser which page to go to
    elem = browser.find_element_by_name("query")#we get the element by name
    elem.clear()
    elem.send_keys(q) #fills out form with our query
    elem.send_keys(Keys.RETURN) #simulates pressing the enter key
    links = browser.find_elements(By.CSS_SELECTOR, '.result-title.hdrlnk') #this will return a list of elements using css selectors
    jobLinks = []

    for link in links:
        #We take the job title and url and add it to a list so we have a list of lists
        jobLinks.append([link.text,link.get_attribute("href")])

    for link in jobLinks:
        browser.get(link[1])
        if len(browser.find_elements(By.CSS_SELECTOR, '.reply_button')):#here we check if there is an apply button with a email we can apply to.
            browser.find_elements(By.CSS_SELECTOR, '.reply_button')[0].click()#here we click the reply button
            time.sleep(5)#tells the program to wait 5 seconds
            email = browser.find_elements(By.CSS_SELECTOR, '.anonemail')[0].text #here we extract the email address 
            title = browser.find_elements(By.CSS_SELECTOR, '#titletextonly')[0].text #here we extract the job title again
        browser.implicitly_wait(40)

Ok now that we can easily find the jobs we want and go to each page we need to send an email to complete this function but before we need to be able to send out the emails. For that we will be using smtplib along with mail which we imported at the start. We will make a new function just to send emails. Lets add this before our getJobs function we will call it sendMail(). We will re-use most of the old parameters from the getJobs function.

def sendMail(sub,rec,res,un,pw,c):
    emaillist = rec#recepient email address
    msg = MIMEMultipart()
    msg['Subject'] = sub
    msg['From'] = un
    msg['Reply-to'] = un

    msg.preamble = 'Multipart massage.\n'

    part = MIMEText(c)
    msg.attach(part)

    #the code below attachs your resume file based
    part = MIMEApplication(open(res,"rb").read())
    part.add_header('Content-Disposition', 'attachment', filename=res)
    msg.attach(part)

    server = smtplib.SMTP("smtp.gmail.com:587")
    server.ehlo()
    server.starttls()
    server.login(un, pw)#here we authenticate our gmail accounts
    #and finally we send the email and log which job we just applied to
    server.sendmail(msg['From'], emaillist , msg.as_string())
    print('applied to '+ sub)

Ok now we just have to call this function inside of the loop we added back inside of getJobs().

def getJobs(q, un , pw, cover, res):
    browser = webdriver.Chrome('chromedriver.exe') #this opens the browser
    browser.get("http://buffalo.craigslist.org/search/jjj") #tells the browser which page to go to
    elem = browser.find_element_by_name("query")#we get the element by name
    elem.clear()
    elem.send_keys(q) #fills out form with our query
    elem.send_keys(Keys.RETURN) #simulates pressing the enter key
    links = browser.find_elements(By.CSS_SELECTOR, '.result-title.hdrlnk') #this will return a list of elements using css selectors
    jobLinks = []

    for link in links:
        #We take the job title and url and add it to a list so we have a list of lists
jobLinks.append([link.text,link.get_attribute("href")])

    for link in jobLinks:
        browser.get(link[1])
        if len(browser.find_elements(By.CSS_SELECTOR, '.reply_button')):#here we check if there is an apply button with a email we can apply to.
            browser.find_elements(By.CSS_SELECTOR, '.reply_button')[0].click()#here we click the reply button
            time.sleep(5)#tells the program to wait 5 seconds
            email = browser.find_elements(By.CSS_SELECTOR, '.anonemail')[0].text #here we extract the email address 
            title = browser.find_elements(By.CSS_SELECTOR, '#titletextonly')[0].text #here we extract the job title again
            sendMail(title,email,res,un,pw,cover)#send the email
        browser.implicitly_wait(40)

Now we need to add in our cover letter we will just use a long string.

coverLetter  =  """I am writing with reference to your Cashier position listed recently on Craigslist. This position appeals to me because of my strong interest in customer service and cash handling. The skills and knowledge I have gained through my education and volunteer experiences will help me perform extremely well for your organization.

As you will note from my enclosed resume, some strengths of mine include:

    ❖ Well versed in operating cash registers, optical price scanners and computers
    ❖ Demonstrated ability to give every customer immediate and undivided attention
    ❖ Skilled in following through on all customer questions and requests
    ❖ Able to keep department supplies filled and maintained
    ❖ Proven record of collecting payments accurately for purchases and make change
    ❖ Track record of maintaining secure custody of all cash, the cash till and any additional items such as register key

The above mentioned attributes combined with my enthusiasm, excellent communication skills and proficiency in MS Word and Excel, make me a perfect candidate for this position. The enclosed resume contains complete details about my cashiering skills and qualifications.

As a passionate and trustworthy cashier, I would like to meet with you to discuss how my skills fit perfectly with your requirements. If you require any additional information in the interim, please give me a call at (716) 777 - 7777 or send me an email at youremail@gmail.com.

Thank you for your time and consideration. I look forward to hearing from you soon.
Sincerely,

Millennial Dave"""

Now all we have to do is call getJobs() with our parameters and our script will apply to jobs for us based on what searched.

getJobs('cashier','youremail@gmail.com','your password', coverLetter, 'youresume.pdf')

Now we can save it as bot.py and our final code should look like this. Just be sure to have your resume inside of the same folder as this script and the chromedriver exe.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import urllib.parse as urlparse
import time
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP
import smtplib

def sendMail(sub,rec,res,un,pw,c):
    emaillist = rec#recepient email address
    msg = MIMEMultipart()
    msg['Subject'] = sub
    msg['From'] = un
    msg['Reply-to'] = un

    msg.preamble = 'Multipart massage.\n'

    part = MIMEText(c)
    msg.attach(part)

    #the code below attachs your resume file based
part = MIMEApplication(open(res,"rb").read())
part.add_header('Content-Disposition', 'attachment', filename=res)
msg.attach(part)

    server = smtplib.SMTP("smtp.gmail.com:587")
    server.ehlo()
    server.starttls()
    server.login(un, pw)#here we authenticate our gmail accounts
    #and finally we send the email and log which job we just applied to
server.sendmail(msg['From'], emaillist , msg.as_string())
print('applied to '+ sub)

def getJobs(q, un , pw, cover, res):
    browser = webdriver.Chrome('chromedriver.exe') #this opens the browser
    browser.get("http://buffalo.craigslist.org/search/jjj") #tells the browser which page to go to
    elem = browser.find_element_by_name("query")#we get the element by name
    elem.clear()
    elem.send_keys(q) #fills out form with our query
    elem.send_keys(Keys.RETURN) #simulates pressing the enter key
    links = browser.find_elements(By.CSS_SELECTOR, '.result-title.hdrlnk') #this will return a list of elements using css selectors
    jobLinks = []

    for link in links:
        #We take the job title and url and add it to a list so we have a list of lists
jobLinks.append([link.text,link.get_attribute("href")])

    for link in jobLinks:
        browser.get(link[1])
        if len(browser.find_elements(By.CSS_SELECTOR, '.reply_button')):#here we check if there is an apply button with a email we can apply to.
            browser.find_elements(By.CSS_SELECTOR, '.reply_button')[0].click()#here we click the reply button
            time.sleep(5)#tells the program to wait 5 seconds
            email = browser.find_elements(By.CSS_SELECTOR, '.anonemail')[0].text #here we extract the email address 
            title = browser.find_elements(By.CSS_SELECTOR, '#titletextonly')[0].text #here we extract the job title again
            sendMail(title,email,res,un,pw,cover)#send the email
        browser.implicitly_wait(40)

coverLetter  =  """I am writing with reference to your Cashier position listed recently on Craigslist. This position appeals to me because of my strong interest in customer service and cash handling. The skills and knowledge I have gained through my education and volunteer experiences will help me perform extremely well for your organization.

As you will note from my enclosed resume, some strengths of mine include:

    ❖ Well versed in operating cash registers, optical price scanners and computers
    ❖ Demonstrated ability to give every customer immediate and undivided attention
    ❖ Skilled in following through on all customer questions and requests
    ❖ Able to keep department supplies filled and maintained
    ❖ Proven record of collecting payments accurately for purchases and make change
    ❖ Track record of maintaining secure custody of all cash, the cash till and any additional items such as register key

The above mentioned attributes combined with my enthusiasm, excellent communication skills and proficiency in MS Word and Excel, make me a perfect candidate for this position. The enclosed resume contains complete details about my cashiering skills and qualifications.

As a passionate and trustworthy cashier, I would like to meet with you to discuss how my skills fit perfectly with your requirements. If you require any additional information in the interim, please give me a call at (716) 777 - 7777 or send me an email at youremail@gmail.com.

Thank you for your time and consideration. I look forward to hearing from you soon.
Sincerely,

Millennial Dave"""

getJobs('cashier','youremail@gmail.com','your password', coverLetter, 'youresume.pdf')

Now we can run our script from the terminal using python bot.py. Hope this was able to help someone find a job or just learn python.

  • Inam Alvi

    can you help me with