It’s painful to see an important ranking drop, at the best of times. It’s worse when you learn the drop was avoidable. Whether the client changed a tag last week and didn’t tell you or your internal team pushed an unexpected update, no matter what size your team, it can be easy for content updates to slip through the cracks. But, clearly, you can’t spend your whole week trawling through changelogs. What if you could be notified of important changes automatically and prevent the problem in the first place? Enter Python, a simple programming language, to make your life a lot easier.
Anita Valentinova gave a great talk on using Python to automate checks at a recent Brighton SEO and she also prepared these really helpful Python scripts, which we’ll be walking through in this post. (We’ve also added some troubleshooting tips and customisation notes at the end.) Once finished, Anita’s scripts will compare a list of your expected page titles and meta descriptions against the live versions of these tags, record any changes in your spreadsheet, and then send you an email to let you know. Handy, right?
First, let’s prepare our spreadsheet of URLs to crawl. For this, we need a list of addresses, and their expected page titles and meta descriptions. (If you haven’t got a URL list already, you can crawl your site with ScreamingFrog SEO Spider.) Then add this information to your sheet where column A is for the URL, B for the Page Title, C for the Meta Description.
Now, it’s time to get started with Python. If this is your first time using Python don’t worry! It’s not too complicated and you won’t have to change anything once you’ve set it up; you’ll just benefit from the results and look great to your team and clients! Here we go:
Before we can run Anita’s scripts, we must install a few extra Python modules. (Essentially, these are groups of functions that mean you don’t have to write your own code.) These are:
It’s really easy to install these; with Python installed, just open the Command Prompt and type ‘pip’ followed by the package name, then press enter. (If you have problems with pip, see ‘Troubleshooting’ below.) It will then download and install the necessary files E.g:
pip install openpyxl
Next, we need to prepare Anita’s ‘config_script_check.py’ file; go ahead and open this in your preferred text editor (I’m using Brackets). The file is made up of 2 parts:
EXCEL_FILE = 'C:\\Username\\Documents\\input.xlsx'
EMAIL_FROM = 'firstname.lastname@example.org' EMAIL_PASSWORD = 'password' EMAIL_SERVER = 'smtp.gmail.com' EMAIL_PORT = 587 EMAIL_TO = 'email@example.com'
And we’re done! Save and close that config file (make sure your spreadsheet is closed too), then double click on ‘meta.py’ to run the script! It should look something like this:
There you have it – check your inbox to see if anything’s changed. All updates are also recorded in a new sheet in your Excel file for you. This is just one use for Python in SEO and there are plenty more, so get involved and automate your checks
Here are a few tips for getting more out of this script, and some things to try in case it breaks:
py -m pip install
If it still does not work, uninstall Python and select ‘Custom install’ when re-installing and select the ‘environment variable’ checkbox (don’t forget to check Add Python to PATH too).
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 2: ordinal not in range(128)
Navigate to your Python install folder, open the Lib folder and locate ‘smtplib.py’. Open this in your text editor and change row 854 to read:
msg = _fix_eols(msg).encode('ascii', errors='ignore')
This should fix the problem 🙂
'From:' 'YOURNAME<' + email_from + '>,'