Python for search engine optimisation Made Straightforward with ChatGPT: A Newbie’s Information – ewebgod

Hi Python Seo 1.jpg

Python is an extremely highly effective programming language that’s not solely fairly straightforward to study but additionally very useful for search engine optimisation professionals. It will probably:

  • Automate repetitive search engine optimisation duties like scraping web sites, parsing knowledge, and producing experiences; this protects you time.
  • Hook up with APIs, permitting you to effortlessly fetch knowledge from sources like ChatGPT API, Google Search Console API, SEMrush, or Ahrefs.

On this information, I’ll present you the right way to use Python programming even when you have no prior expertise! 

We’ll create the next a number of useful instruments: 

  • software that scrapes a sitemap file and saves URLs with errors (404, 5xx) to a separate file.
  • scraper that extracts <title> and meta description tags from an inventory of URLs.
  • software that connects to the PageSpeed Insights API to collect efficiency knowledge a few given web page. 
  • program that performs fundamental manipulations with a CSV file.
  • program that mixes knowledge from Google Search Console and Google Analytics.
  • useful software that makes use of the ChatGPT API and saves the ends in a CSV file.

And the perfect half? You don’t want any prior programming expertise! Let’s dive in and get began!

What you’ll want to begin programming instantly 

To run Python applications you want a Python atmosphere. For the aim of this tutorial, we shall be utilizing Google Colab. It’s superior for 3 causes: 

  • It’s very straightforward to start out. Creating your first Python program in it is going to take you simply a few minutes (or a minute, when you’re quick). 
  • You don’t want to put in something in your pc. 
  • You’ll be able to work on the identical code with your mates or colleagues. Consider Google Docs however for Python programming. In the event you battle with a selected program, you possibly can ask your colleague for assist. 

However don’t fear we’ll take it step-by-step. 

Right here’s the deal!

To make sure your success, you’ll want to comply with these pointers: 

  • Learn the chapter totally, so that you don’t miss any essential steps.
  • Encounter a Python error? Ask ChatGPT to repair it.
  • No luck? Begin a brand new ChatGPT chat to resolve the problem.
  • If ChatGPT struggles along with your CSV information, present extra context and a pattern line with the information. Clarify the file’s construction, columns, and knowledge format.
  • In case the Python program crashes, request ChatGPT so as to add exception dealing with.
  • If ChatGPT offers you incomplete enter and finishes at a random place (sure, it occurs!), simply ask ChatGPT to start out from the place it completed.


Writing your first Python program – “hi there world”

All programmers start their journey in a similar way. They create a easy pc program designed to do one easy factor: show “Hi there world” on the display.

Let’s do it! 

To get began, request ChatGPT to generate a Python program for you:


Write a Python program that may print: “hi there world” on the show

Then copy the code supplied by ChatGPT. You are able to do this by clicking on the: “Copy code” button.

Subsequent, open Google Colab (it’s a particular atmosphere the place you possibly can run your Python applications) and choose “New pocket book” to create a contemporary workspace.

Paste the copied code into the Google Colab cell (confer with the screenshot beneath for steerage) and hit the “Run” button:

Congratulations! You’ve simply written your very first Python program! It printed “hi there world” on the display. 

Write a program that downloads a sitemap

Alright, let’s transfer on to one thing extra sensible. How about we create a program that downloads all of the URLs from a sitemap and saves them to a textual content file?

With ChatGPT, you possibly can create this program in simply a few minutes.

Kind the next immediate into the chat:


Write a Python program that downloads all of the URLs in a sitemap, particularly, and performs
the next duties:

  • Save all of the URLs in a file named “sitemap.txt”.
  • Go to all of the URLs within the sitemap, as much as a most of 100 URLs, and test their standing codes. If a URL doesn’t return a 200 OK standing code, put it aside to a file named “broken_urls.csv”, which can comprise two fields: the URL and its corresponding HTTP standing code.

When you’ve obtained the code from ChatGPT, simply as you probably did with the “Hi there World” instance, hit the “Copy code” button positioned on the top-right nook of the ChatGPT console.

Now, in your Google Colab pocket book, the principle cell is already full of the earlier program. 

You’ve three choices for dealing with this:

  1. Take away the outdated code and paste within the new one.
  2. Create a brand new pocket book.
  3. Insert a brand new cell.

For simplicity’s sake, let’s simply add new cells to the identical pocket book.

To do that, in Google Colab click on on “Insert” after which choose “Code cell”.

Go forward and paste the code you bought from ChatGPT into a brand new Google Colab cell, then click on the “Run” button.

In case your sitemap is comparatively small, in a few seconds Google Colab will notify you that it efficiently completed the duty:

The place is the generated file?

Within the immediate, you requested ChatGPT to obtain a full checklist of URLs and save them to a textual content file. To search out this file in Google Colab, merely click on the folder icon on the left, and right here you’ll shortly spot the sitemap.txt file.

You’ll be able to then open the file to see if it was correctly generated: 

Now, let’s discover some methods to boost this program:


Make it extra bullet-proof: Python applications can crash if for some purpose they’ll’t go to sure URLs. To make sure easy operation, ask ChatGPT to generate code that may deal with errors. ChatGPT can provide you new Python code that may warn you if one thing goes improper however received’t break the software. Right here’s a pattern: Write a program that may extract <title> and meta description from an inventory of URLs. 

Utilizing Python we are able to simply write a program that may extract fundamental info (corresponding to <title> and <meta> description from an inventory of pages). 

Write a program that may extract <title> and meta description from an inventory of URLs. 

Utilizing Python we are able to simply write a program that may extract fundamental info (corresponding to <title> and <meta> description from an inventory of pages). 

Right here’s a pattern immediate:


Write a Python program that:

  • goes by means of an inventory of URLs saved within the “enter.csv” file,
  • extracts the title and meta description,
  • saves the outcomes to a “custom_extraction.csv” file. The file ought to have three columns: URL, title, and meta description.

Make it possible for this system handles any errors which will happen throughout execution. Write a proof for each bit of code as a remark.

ChatGPT shortly generated a Python program to perform the duty:

As earlier than, copy the code generated by ChatGPT and add a brand new code cell to your Google Colab pocket book. 

When you paste the code, click on the “Run” button.

If you attempt to use it, you’ll encounter an error saying the file doesn’t exist. To repair this, add the “enter.csv” file containing the URLs you need to scrape.

Click on the “folder” icon from the menu after which add the file. In the event you don’t need to use your individual file, use my sample

After importing the file, this system ought to run easily, extracting the <title> and meta description values. As anticipated, the outcomes shall be saved within the “custom_extraction.csv” file:

Nice work, ChatGPT, and Python!

Write a program that measures the pace of a web site

You’ve already discovered lots! 

Now, let’s dive into one thing extra superior. We’ll be utilizing the PageSpeed Insights API to collect efficiency details about a number of URLs in bulk. 

Vital word: 

Now we’re coming into a area the place ChatGPT can generate totally different applications for each individual. A few of them may match within the first run, some received’t. If one thing is improper with the output don’t surrender 🙂 


  • Telling ChatGPT that this system just isn’t working and asking for a rewrite.
  • Asking ChatGPT to create this program, however this time, in a brand new chat.
  • In the event you use ChatGPT Plus, swap to ChatGPT-4 – it ought to generate higher output.
  • If none of this works, put my applications into Google Colab. Here’s the hyperlink.

So no worries, we’ll make it work 🙂 

Writing a program that may hook up with Google PageSpeed API could seem sophisticated at first, however the perfect half is that the immediate is brief and fairly intuitive:


Write a program that may take an inventory of URLs from enter.txt
and for every of the URLs it is going to get the PageSpeed Insights rating.
Then save the outcomes to the psi_api_output.csv file
with an important metrics.

As anticipated, ChatGPT generated this program for me:

Subsequent, copy and paste the code into a brand new cell in your Google Colab pocket book.

Earlier than you run it, please put your PageSpeed Insights API key into the code.

As an example, in case your secret’s: “myKey43434343”, then the road ought to appear like this: api_key = “myKey43434343”

In case you don’t have the PageSpeed Insights API key, you possibly can receive it from: 

When you enter the code and put your individual API key in, click on on the “Run” button once more

Sadly, I encountered an error. Oh no!

Don’t fear, we’re sticking to a no-code method for this tutorial. 

So let’s ask ChatGPT to assist with the error:

ChatGPT defined the reason for the error and supplied steps to repair it. Observe: I trimmed the screenshot for brevity. 

On this case, ChatGPT suggests changing the get_pagespeed_insights_score perform with a brand new, improved model. You’ll be able to edit the code identical to you’d in a textual content editor, ensuring to preserve the indentation (which is important in Python).

Alternatively, when you’re pressed for time, or simply unsure the place precisely you need to paste this, ask ChatGPT to generate the total program once more, and you may change your entire code so you possibly can copy/paste it immediately into the Google Colab Pocket book:

After pasting the brand new code into Google Colab, it really works completely! 

For a fair higher program, contemplate the next concepts:


  • Ask ChatGPT to show present progress. (In my case, this system took three minutes for simply 15 URLs. It may be a protracted watch for bigger URL lists, so having a visible indication of progress is useful).
    Pattern immediate: “show present progress, utilizing the TQDM Library
  • Request particular efficiency metrics from the PageSpeed Insights API: Ask ChatGPT to retrieve specific metrics to fit your wants. Pattern metrics that may be helpful: First Contentful Paint, Pace Index, Time to Interactive, First Significant Paint.

Write a program for extracting only one column of knowledge

Now think about you will have a CSV file with numerous knowledge, however you solely need to extract a single column (as an illustration, a column with an inventory of URLs).

Then you definitely do it in Excel, and it crashes. You strive once more and… it crashes.

Overlook Excel; you are able to do it in Python. Right here’s the immediate you should use: 


I’ve a CSV file named ‘gsc_data.csv’ that accommodates three columns: URL, Clicks, and CTR. Every column is separated by a comma.

  • Please write a Python program that extracts solely the primary column, which accommodates URLs, and saves it to a separate file named “urls_only.txt”. This system ought to deal with circumstances the place some knowledge is lacking for sure URLs.
  • After working this system, I would really like it to provide me a brief report in regards to the steps that this system has achieved.

You already know the workflow:

  • Ask ChatGPT to generate this system. 
  • Copy the output generated by ChatGPT.
  • Insert a brand new code cell in Google Colab.
  • Add the input file into Google Colab. 
  • Paste the code and click on on “Run”. 

As anticipated, this system extracted 21 URLs from ‘gsc_data.csv’ and saved them to ‘urls_only.txt’. Beneath you possibly can see the preview of this file: 

Write a program that mixes two information into one

One other space the place ChatGPT may be useful is to generate a program that mixes two information into one.

Think about you will have two separate CSV information, one coming from Google Search Console, and the other coming from Google Analytics. Excel might shortly fail with bigger volumes. 

Right here’s a pattern immediate: 


Please write a Python program that joins knowledge from two information:

1. gsc_data.csv

2. ga_data.csv

based mostly on the URL column

  • each current comma-separated values
  • If some knowledge is lacking in both desk, please current it with an empty worth
  • and ensure this system works fantastic with any columns
  • it ought to save the information to an exterior file named: combined_gsc_ga_data.csv

ChatGPT shortly generated the Python program that may be a part of two CSV information: 

I believe you already know what to do now. In case you hesitate: 

  • Copy the output from ChatGPT.
  • Paste the output to the brand new code cell in Google Colab.
  • Add enter information [1] [2] to Google Colab. For the aim of this tutorial, use information supplied by me [1] [2] In case you need to use your individual information, make sure that:
    – Each information comprise the URL area.
    – Each information comprise URLs in the identical format.
  • Run the code cell.

This Python program completely joined the information, as proven within the screenshot beneath:

Concepts for additional enchancment of this system: 

  • Make it possible for the information from each sources is homogeneous. Normally, Google Analytics knowledge will current a relative URL whereas Google Search Console will at all times current the total URL. If this occurs, this system will fail. In such circumstances, first instruct ChatGPT so as to add a “https://www” prefix to each URL from the ga_data.csv after which do the remainder. 
  • Ask ChatGPT to research the information extra totally. As an example, it’s possible you’ll need to discover URLs that guests spend a number of time on, however… with zero clicks from Google customers,.  This may present you an inventory of URLs which are attention-grabbing to your customers, however both aren’t listed, or have some main rating issues. 

Utilizing ChatGPT API

With the facility of the ChatGPT API, you possibly can carry out varied duties in bulk, making them extra environment friendly and time-saving. Right here’s how a number of the examples talked about earlier may be executed in bulk:

  • Summarize your articles in bulk: You’ll be able to create summaries or meta descriptions for a lot of articles without delay.
  • Test content material high quality in bulk: Ask ChatGPT to routinely undergo an inventory of your articles to see how good your web site’s content material is.
  • Repair <title> tags in bulk: You’ll be able to ask ChatGPT to undergo an inventory of URLs, extract title tags and make ChatGPT optimize your web site’s <title> tags higher for extra clicks and conversions.
  • Appropriate grammar in bulk: Repair grammar errors in a number of weblog articles all of sudden.

Now, observe! Let’s create a program utilizing ChatGPT that:

  • Reads an enter.txt file containing an inventory of URLs.
  • For every URL, analyzes the <title> tag to find out the probability of customers clicking on it in search outcomes.

This fashion, you possibly can optimize your web site extra effectively and successfully by understanding the efficiency of every <title> tag.

Right here’s a pattern immediate:


Write a Python program that may learn an inventory of URLs from enter.txt.

  • For every URL, it is going to extract the meta description and <title> tags.
  • Subsequent, it is going to use the ChatGPT API to guage the <title> on a scale of 0-10, figuring out how seemingly it’s to encourage individuals to click on on the search end result.
  • This system also needs to present a proof for every judgment.
  • The outcomes needs to be saved in a CSV file named title_score.csv.

As at all times, ChatGPT confronted the problem to generate such a program! 

Comply with these easy steps to get began:

  • Copy the output from ChatGPT.
  • Paste the output into a brand new code cell in Google Colab.
  • Modify the code to embody your OpenAI API key.


Error: lacking module

In the event you encounter an error stating that the “openai module just isn’t discovered”, you’ll want to put in it. To take action, merely add the command !pip set up openai earlier than the ChatGPT output and run the cell once more.

This system gave me the anticipated file with the outcomes: 


Making the output higher

In the event you discover the output high quality isn’t nice, there are a number of methods to enhance it:

  • Refine the immediate: rewrite the immediate to particularly ask ChatGPT for concepts to boost your titles.
  • Experiment with totally different ChatGPT fashions within the calls to ChatGPT API: on this case, the Python file generated by ChatGPT makes use of the text-davinci-002 mannequin, which relies on GPT-2. You’ll get MUCH higher outcomes with GPT-3 or GPT-4 fashions.

Vital word: 

It’s unclear what logic ChatGPT follows whereas judging the clickability of pages. Sadly, it’s extra of a black field, like Google. 

If you wish to have extra predictable outcomes, use ChatGPT custom training techniques. As at all times, make certain it really works in your area of interest!

Asking ChatGPT to clarify Python code to you 

In the event you’re new to Python and search engine optimisation, all of the Python applications on the market can appear overwhelming. However don’t fear, there’s a simple strategy to perceive what a selected Python code does!

Simply ask ChatGPT to clarify it to you. Right here’s a pattern immediate:


Please clarify what the Python code beneath is supposed to do

Right here’s my Python code:

Be certain that the reason is written in easy phrases that anybody can perceive.

In my case, I requested ChatGPT to clarify one of many applications supposed to extract the <title> and meta description tags that we generated earlier:

Wrapping up

I hope my directions have helped you start programming in Python. Now, it’s time so that you can put what you’ve discovered into motion.

Consider some applications that may assist you in your on a regular basis search engine optimisation job and make use of chatGPT that will help you write them!

Have enjoyable!

So, don’t hesitate to make use of this function anytime you want a little bit assist!


This information is only a small a part of my FREE e book: Mastering ChatGPT for search engine optimisation.  Grab a copy!

#Python #search engine optimisation #Straightforward #ChatGPT #Freshmen #Information

Leave a Reply

Your email address will not be published. Required fields are marked *