Python is one of the top programming languages today. Beginners who have learned it describe the ease and quickness of mastering its syntax. Experts of Python brag about its versatility. Machine learning, data analytics, web page development, and even hardware applications are possible to process and compute.
AI is an emerging area being used to analyze data and patterns, faster than human capabilities. Google Organic Search utilizes machine learning techniques to perfect its search engine results. Its RankBrain algorithm is capable of analyzing web pages and visitor clicks to determine keyword search intent and more accurately match relevant websites. This impacts traditional SEO tools that marketing professionals rely upon. Those who work in search engine optimization might find it helpful to use machine learning techniques to decipher machine learning algorithms. Python makes a good choice to complement analyses from these tools while also automating other tasks.
Here is a collection of Python scripts that provide deeper and meaningful SEO analytics that traditional SEO tools may not uncover.
Generate Text from Images Using Python
Are there missing ALT attributes on your pages? Instead of manually populating the attribute for each image, this handy Python script might be more useful. It uses a Python library called Pythia (created by Facebook). The library uses a “neural network” approach to produce sensible text based on the provided image URL. To learn more about installing and implementing the script, read the Search Engine Journal article here.
Optimize the Image File Size
Image file size (different than “image dimension”) contributes to the page speed of a web page. The ideal maximum length of an image file should be 200kb. Many tools exist to shrink image file sizes but they require manual uploading. Python can provide an automated, programmatic solution. This script by Victor Domingos gives you multiple options that can shrink an image’s file size such as reducing colors, reducing image dimensions, changing file formats, deleting EXIF data, and defining JPG quality levels.
Log File Analysis for SEO
Log file analysis is a useful skill for SEO professionals. It can provide deeper insights that a typical crawl tool might not provide. For example: if you’re detecting a lot of 404 errors then you can use log file analysis to determine the source URL(s) of the issue. Ian Lurie of Portent.com gives a small list of SEO insights that can be gleaned from log files:
- Spider traps. Log files give you a great look at how search bots are crawling your site.
- Spam content. If some hacker dumped a bunch of pages listing porn links on your site, any clicks to those pages appear in the log files.
- Broken external links. Google eventually gives up crawling broken links from other sites. But people still click them. Track down those busted external links and reclaim some authority.
- Incorrect server responses. Google Search Console can show some soft 404 errors, but the log file shows you all of them.
Log files record server codes and instances, but reading these files and retrieving useful data would be a challenge and take time.
One developer has discovered a Python programming method to create useful log file data at scale.
Moshe Ma-yafit wrote a Medium article describing log file analysis using the Python programming language (actual script link here). The script can retrieve data from log files and then allows you to output visuals about:
- Server response codes
- Google bot hits
- 404 errors per referrer URL
The above list represents only a few examples. You can read the article to learn more about the process of using Python and the log file analyzer script.
Search Engine Land provides a different Python method for performing log file analysis. Author Charly Wargnier provides a detailed example of how to utilize the power of Google Cloud to run a Python analytical script that gleans insights from log files.
HREFLANG Tag Validation
HREFLANG is a meta HTML tag attribute that’s used to indicate the target country and language for a web page. It’s intended for websites which have multiple versions of their web pages transcribed in different languages for multiple countries.
Problems arise when the HREFLANG attribute is targeting the wrong country and/or language for a web page. Validating the attribute for multiple pages is a nightmare if someone validates it manually. A scalable solution would not only be quicker, but it would also remove the human checking error as well. Luckily, Python comes to the rescue.
SEO company Distilled released an open-source Python library called polly that checks HREFLANG attributes. You can install it to your local server and then run the tool, but another option is to use the online version of the tool at http://hreflang.ninja that’s also built with the library. For instructions about using the library, you can read this article on Moz.com.
Calculating Keyword Growth
Keyword research is still an important task. You can use it to discover topics and how people search for things on Google. One aspect that’s important to know about a keyword is whether there’s been growth or decline of its average monthly search volume. This indicates growing interest or growing disinterest with a keyword. It’s simple to analyze this per keyword, but doing this for multiple keywords simultaneously would take a long time. This Python script from SearchWilderness.com provides a programmatic solution involving the Python library pandas and Google Trends.
Google Search Console Data Collections and Analyses
This article from HackerNoon.com explains SEO analysis scenarios by using Python. It’s possible to analyze keyword SERP positions, impressions, and clicks—to determine if there are abnormal performance variations with a keyword, which keywords are top performers, new and lost keywords, and which keywords exhibit under-performing Click-Thru-Rates. The script relies on the Python library pandas and performance data from Google Search Data.
It’s also useful to categorize the keyword data that Google Search Console is able to capture. Search Engine Land published an article by JR Oakes that shows how an analyst can categorize keywords by tapping into the Alpriori algorithm using Python. If you’ve never heard of the algorithm, here’s an explanation from the author:
The Apriori algorithm was proposed by RakeshAgrawal and RamakrishnanSrikant in 2004. It was essentially designed as a fast algorithm used on large databases, to find association/commonalities between component parts of rows of data, called transactions.–JR Oakes
After categorizing the data, Oakes continues to demonstrate how to use BERT embeddings to find semantically related categories and then shows an example of how to plot position growth and decline per semantic category.
According to the author of this Python script:
Most keyword ranking tools will tell you which pages your domain is ranking for, but not your competitor’s highest-ranking pages per keyword…we aren’t tracking page performance for the long-term. We’re simply trying to get quick data.—John McAlpin
Suppose you’re trying to figure out how many times a competitor’s list of pages ranks for a list of keywords. How many times does a page appear and for which rank positions? Instead of aggregating multiples of data sources, this Python technique is able to automatically gather the data for you. Combine it with this Data Studio template (using Google Sheets) to generate a visually pleasing chart. You’ll need the following libraries: urllib, lxml, and requests.
Automating Schema.org Generation
The Python script exhibited in the article (link) can be adapted to automatically generate any type of Schema. In this example, it’s used to generate VideoObject using the following libraries: Genson, Selenium, and BeautifulSoup. There’s also some manual formatting of your spreadsheet file required but it’s not too hard.
Automate Keyword Research
This detailed tutorial utilizes pre-defined coding rules to automatically pull keywords from a keyword research report. Also, the script is capable of detecting Google Ads for each keyword and calculating keyword ranking difficulty and search volume trends. According to the author of the article, the agency that developed the script decreased their keyword research time from 2 hours to 10 minutes.
Keyword intent is an important determination when you’re doing keyword research. One way to conduct success keyword research is to also group keywords by intent. For hundreds and thousands of keywords, this task can take hours if it’s performed manually. It’s possible to use Python and machine learning techniques to automate the process. Hamlet Batista wrote an article on Search Engine Journal that describes how to combine Python, TensorFlow, BigQuery, and Data Studio for implementing the BERT technique. This allows for a more accurate analysis since it takes into account the context of a word phrase and not just relying on the presence of certain modifier words (for example: “where is the nearest safeway” has the same intent as “safeway within 1 mile”).
Another useful aspect of Python for SEO is uncovering clues about how Google classifies your website. This can help decide what type of content to create and thus which keywords to focus on when conducting keyword research. JR Oakes once again has created a handy method for helping uncover that insight. His method involves utilizing the Google Natural Language API to apply sentiment and categorization scoring. You can use this for analyzing your own pages or those of your competitors.
Content Scraping for Site Analysis
Content scraping is a popular usage of Python. Normally the activity is reserved for programmers trying to retrieve data values from a web page but according to famed marketer Neil Patel, SEO professionals can use content scraping for the following purposes:
- Scrape organic keyword results
- Search Google Ads copy for inspiration
- Find potential influencers with blog comments
- Use data feeds for guest blogging research
- Determine your best performing categories
- Find content on forums to create backlinks
- Get more data for your blog posts
The author also provides content scraping tips and advice plus warns marketers not to use it for copying content from other pages.
However, Neil Patel’s article doesn’t provide Python solutions. It recommends using other software programs, but a Python solution can give you endless options. Two popular libraries for content scraping is requests and BeautifulSoup. Examples are provided in the automated Schema generation and competitive analysis sections above that utilize both of these libraries.
Extra: Calculating Internal PageRank (R Language)
This involves an SEO concept called “internal linking.” How web pages are linked together on a website determines its structure and it’s an important optimization factor of SEO. Calculating your internal PageRank scores can provide a quantitative way of judging the effectiveness of your internal link structure.
Although this method doesn’t utilize Python, it’s nonetheless a useful technique to gauge the effectiveness of your internal links structure. A Search Engine Land article written by Paul Shapiro details the steps of using the R programming language to calculate the PageRank score for a website. The article also provides real-life applications of using PageRank to plan internal links.
Python libraries that might provide functionality similar to the R approach are NetworkX, Fast PageRank, and iGraph for Python. The first is a popular library that has a built-in function specifically for PageRank link analysis. The second is a library created by Armin Sajadi for use on his WikiSim project. They both, at least by appearance, depend on referencing URLs by numerical indices (ex: https://yahoo.com = , https://google.com = ). The iGraph library also exists for Python so you might be able to produce the same output display. No practical SEO example could be found, but if you tinker with the Python libraries for a while maybe you could produce a Python version of the methods in Paul Shapiro’s demo. Just a thought.
Python is a popular programming language for data analysis. It can uncover deeper insights that traditional SEO tools aren’t able to provide. If you work in search engine optimization and data needs are becoming complex, Python is a programming language you might want to try.
The fastest way of learning how to code with Python is by taking an online course. Compare available courses from learning platforms such as Udemy, Coursera, Edx, and others.
Correction: the link that references Paul Shapiro’s demo of analyzing internal links is based on the R programming language, not Python. This has been corrected in the blog post.