Controlling The Web With Python - Towards Data Science
Controlling The Web With Python - Towards Data Science
William Koehrsen
Data Scientist at Feature Labs, Data Science Communicator
Mar 11 · 9 min read
https://towardsdatascience.com/controlling-the-web-with-python-6fceb22c5f08 1/14
2018/9/30 Controlling the Web with Python – Towards Data Science
Obligatory XKCD
. . .
https://towardsdatascience.com/controlling-the-web-with-python-6fceb22c5f08 2/14
2018/9/30 Controlling the Web with Python – Towards Data Science
a limitless number of situations. (If you want to see the complete code,
it’s available on GitHub).
Approach
Before we can get to the fun part of automating the web, we need to
gure out the general structure of our solution. Jumping right into
programming without a plan is a great way to waste many hours in
frustration. I want to write a program to submit completed course
assignments to the correct location on Canvas (my university’s
“learning management system”). Starting with the basics, I need a way
to tell the program the name of the assignment to submit and the class.
I went with a simple approach and created a folder to hold completed
assignments with child folders for each class. In the child folders, I
place the completed document named for the particular assignment.
The program can gure out the name of the class from the folder, and
the name of the assignment by the document title.
Here’s an example where the name of the class is EECS491 and the
assignment is “Assignment 3 — Inference in Larger Graphical Models”.
dir_list = list(os.listdir(submission_dir))
https://towardsdatascience.com/controlling-the-web-with-python-6fceb22c5f08 3/14
2018/9/30 Controlling the Web with Python – Towards Data Science
print(file_tup)
This takes care of le management and the program now knows the
program and the assignment to turn in. The next step is to use selenium
to navigate to the correct webpage and upload the assignment.
import selenium
When we open the Canvas webpage, we are greeted with our rst
obstacle, a login box! To get past this, we will need to ll in an id and a
password and click the login button.
https://towardsdatascience.com/controlling-the-web-with-python-6fceb22c5f08 4/14
2018/9/30 Controlling the Web with Python – Towards Data Science
Imagine the web driver as a person who has never seen a web page
before: we need to tell it exactly where to click, what to type, and which
buttons to press. There are a number of ways to tell our web driver
what elements to nd, all of which use selectors. A selector is a unique
identi er for an element on a webpage. To nd the selector for a
speci c element, say the CWRU ID box above, we need to inspect the
webpage. In Chrome, this is done by pressing “ctrl + shift + i” or right
clicking on any element and selecting “Inspect”. This brings up the
Chrome developer tools, an extremely useful application which shows
the HTML underlying any webpage.
To nd a selector for the “CWRU ID” box, I right clicked in the box, hit
“Inspect” and saw the following in developer tools. The highlighted line
corresponds to the id box element (this line is called an HTML tag).
This HTML might look overwhelming, but we can ignore the majority
of the information and focus on the id = "username" and
name="username" parts. (these are known as attributes of the HTML
tag).
To select the id box with our web driver, we can use either the id or
name attribute we found in the developer tools. Web drivers in
selenium have many di erent methods for selecting elements on a
webpage and there are often multiple ways to select the exact same
item:
# Equivalent Outcome!
id_box = driver.find_element_by_id('username')
Our program now has access to the id_box and we can interact with
it in various ways, such as typing in keys, or clicking (if we have
selected a button).
https://towardsdatascience.com/controlling-the-web-with-python-6fceb22c5f08 5/14
2018/9/30 Controlling the Web with Python – Towards Data Science
# Send id information
id_box.send_keys('my_username')
We carry out the same process for the password box and login button,
selecting each based on what we see in the Chrome developer tools.
Then, we send information to the elements or click on them as needed.
# Send password
pass_box.send_keys('my_password')
# Click login
login_button.click()
https://towardsdatascience.com/controlling-the-web-with-python-6fceb22c5f08 6/14
2018/9/30 Controlling the Web with Python – Towards Data Science
courses_button.click()
The program nds the correct class using the name of the folder we
stored in the rst step. In this case, I use the selection method
find_element_by_link_text to nd the speci c class. The “link text”
for an element is just another selector we can nd by inspecting the
page. :
This work ow may seem a little tedious, but remember, we only have to
do it once when we write our program! After that, we can hit run as
many times as we want and the program will navigate through all these
pages for us.
https://towardsdatascience.com/controlling-the-web-with-python-6fceb22c5f08 7/14
2018/9/30 Controlling the Web with Python – Towards Data Science
At this point, I could see the nish line, but initially this screen
perplexed me. I could click on the “Choose File” box pretty easily, but
how was I supposed to select the actual le I need to upload? The
answer turns out to be incredibly simple! We locate the Choose File
box using a selector, and use the send_keys method to pass the exact
path of the le (called file_location in the code below) to the box:
That’s it! By sending the exact path of the le to the button, we can skip
the whole process of navigating through folders to nd the right le.
After sending the location, we are rewarded with the following screen
showing that our le is uploaded and ready for submission.
https://towardsdatascience.com/controlling-the-web-with-python-6fceb22c5f08 8/14
2018/9/30 Controlling the Web with Python – Towards Data Science
Cleaning Up
File management is always a critical step and I want to make sure I
don’t re-submit or lose old assignments. I decided the best solution was
to store a single le to be submitted in the completed_assignments
folder at any one time and move les to a submitted_assignments
folder once they had been turned in. The nal bit of code uses the os
module to move the completed assignment by renaming it with the
desired location:
https://towardsdatascience.com/controlling-the-web-with-python-6fceb22c5f08 9/14
2018/9/30 Controlling the Web with Python – Towards Data Science
The program provides me with a chance to make sure this is the correct
assignment before uploading. After the program has completed, I get
the following output:
While the program is running, I can watch Python go to work for me:
Conclusions
The technique of automating the web with Python works great for
many tasks, both general and in my eld of data science. For example,
we could use selenium to automatically download new data les every
day (assuming the website doesn’t have an API). While it might seem
like a lot of work to write the script initially, the bene t comes from the
fact that we can have the computer repeat this sequence as many times
as want in exactly the same manner. The program will never lose focus
and wander o to Twitter. It will faithfully carry out the same exact
https://towardsdatascience.com/controlling-the-web-with-python-6fceb22c5f08 10/14
2018/9/30 Controlling the Web with Python – Towards Data Science
series of steps with perfect consistency (which works great until the
website changes).
https://towardsdatascience.com/controlling-the-web-with-python-6fceb22c5f08 11/14
2018/9/30 Controlling the Web with Python – Towards Data Science
https://towardsdatascience.com/controlling-the-web-with-python-6fceb22c5f08 12/14
2018/9/30 Controlling the Web with Python – Towards Data Science
https://towardsdatascience.com/controlling-the-web-with-python-6fceb22c5f08 13/14
2018/9/30 Controlling the Web with Python – Towards Data Science
https://towardsdatascience.com/controlling-the-web-with-python-6fceb22c5f08 14/14