A Guide To Google’s Knowledge Graph Search API For SEO via @sejournal, @rvtheverett

Overtake your competitors by gaining a deeper understanding of how your users find you online. With this Python code, you can increase your confidence score. The post A Guide To Google’s Knowledge Graph Search API For SEO appeared first...

A Guide To Google’s Knowledge Graph Search API For SEO via @sejournal, @rvtheverett

Google introduced the Knowledge Graph in 2012 to help searchers discover new information quicker.

Essentially, users can search for places, people, companies, and products and find instant results that are most relevant to the query.

The Knowledge Graph is a collection of topics, also known as entities, connecting to other entities. Entities are single information objects that can be uniquely defined.

They enable Google to go beyond just keyword matching when returning a response to a particular query. This is further helping Google towards its goal of becoming an answer engine.

Google will show Knowledge Graph data within SERP features such as knowledge panels, knowledge cards, and featured snippets.

This can help brands become more visible in search results and build authority for certain topics. Structured data on websites can influence data pulled into the Knowledge Graph.

Google uses the Knowledge Graph to provide a better search experience for users as it can better understand different topics and their relationships to each other.

For example, if we want to see a film’s cast, Google can display this in a carousel format on the search results page.

Example of CarouselScreenshot from Google, September 2022

However, these SERP (search engine results page) features can also lead to fewer website clicks, as Google can show much more information on the search result page.

This enables them to deliver a fast and accurate response for searchers and direct them towards other search results, with features such as “People also search for” and relevant queries related to the main search term.

For example, if we take the K-pop group BTS, within a single search, I can see a list of all of the members, their songs and albums, as well as upcoming events, awards they have won, and the different places I can listen to their music.

All in one search without having to visit a single external website.

BTS SERP Feature ExampleScreenshot from Google, September 2022

The Knowledge Graph API

The Knowledge Graph API, which Google has built, enables us to find entities within the Google Knowledge Graph for certain queries.

It gives us direct access to the database to see the entities marked up for each query. It is also independent of the user’s location, providing a more accurate view of the Knowledge Graph.

Some example use cases of the API, as given by Google, include:

Getting a ranked list of the most notable entities that match certain criteria. They are predictively completing entities in a search box. Annotation/organizing content using the Knowledge Graph entities.

As the documentation states, the API itself returns only individual matching entities rather than graphs of interconnected entities.

Using Python To Call The API

There are four different clients that Google enables the API to be called via: Python, Java, JavaScript, and PHP.

An example starting point for each can be found on the relevant page in the documentation.

For this example, I will use Python as it is the language I am most familiar with.

Creating An API Key

The first step is creating an API key to send a request to the API.

To generate an API key, go to the Google API console and navigate to the credentials page.

The next step is to go to the API library, search for Knowledge Graph, and then enable it.

Knowledge Graph API,Screenshot from Knowledge Graph API, September 2022

You can save a note of your API key, but you are also able to easily find the API key again by clicking on the API that you had already generated.

Credentials Knowledge Graph APIScreenshot from Knowledge Graph API, September 2022

A Simple API Request

To return entities matching a query, together with the results score for each entity, there is a simple piece of Python code that you can run, either in Google Colab (easily accessible for beginners) or in your local environment.

api_key = ' ' #add your API key query = 'BTS' #add your query service_url = 'https://kgsearch.googleapis.com/v1/entities:search' params = { 'query': query, 'limit': 10, 'indent': True, 'key': api_key, } url = service_url + '?' + urllib.parse.urlencode(params) response = json.loads(urllib.request.urlopen(url).read()) for element in response['itemListElement']: print(element['result']['name'] + ' (' + str(element['resultScore']) + ')')

This will produce an outcome like the below:

API ResponseScreenshot from Google Colab, September 2022

Within this, we can set a couple of parameters, depending on what we’re looking for.

The first thing you will need to add is your API key, followed by the query for which you would like to generate the results.

The parameters are then set to call the API key you have already added and the query you have selected.

This enables you to easily change the query you are searching for each time you run the code.

Then we have the limit, which is the number of entities you want to return. The default for this is 20, with a maximum of 500. Remember that requests with high limits have a higher chance of timing out.

Then we can use a Boolean (True or False) to decide whether we want to indent the JSON response for easy formatting.

There are other parameters that you can include, such as:

Languages: a list of the language codes to which you want to limit the response. Types: used to restrict the entities to those of the type you choose, e.g., if you only want ‘Person’ entity results.

We then ask the script to call the URL, complete the request and parse the result to a simple print of the entity name and result score for each entity, which will be enclosed in parenthesis.

Extracting Even More

Returning the entities and their result score is just scratching the surface. There is so much more we can get from the Knowledge Graph API.

We can return a JSON object containing all the response fields stored for each entity with a few more lines of code and some functions.

First, we want to request a return of the session’s page that will be searched through the API.

def get_source(url): try: session = HTMLSession() response = session.get(url) return response except requests.exceptions.RequestException as e: print(e)

Then, using a similar API request as in the original code, we can call it in conjunction with our query request using the same parameters.

def knowledge_graph(api_key, query): query = 'BTS' #add your query service_url = 'https://kgsearch.googleapis.com/v1/entities:search' params = { 'query': query, 'limit': 10, 'indent': True, 'key': api_key, } url = service_url + '?' + urllib.parse.urlencode(params) response = get_source(url)

Then, we enter our API key to return our response object with the full data.

return json.loads(response.text) api_key = " " #add your API key knowledge_graph_json = knowledge_graph(api_key, query) knowledge_graph_json

To see the results a little easier and help make more sense of the response, we can normalize the JSON object into a Pandas DataFrame. This will take each field and transfer it into a column, with each entity a different row.

pd.json_normalize(knowledge_graph_json, record_path=’itemListElement’)

A Guide To Google’s Knowledge Graph Search API For SEO

I also found it interesting to run this code on different days with the same query and review how the results change.

Response Fields

Several fields will be extracted for each entity within the Knowledge Graph API:

id: the canonical URI for the entity. name: the name of the entity. type: a list of supported schema types that match the entity. description: a short description of the entity. image: an image that is related to the entity. detailedDescription: a detailed description of the entity. url: the official website of the entity. resultScore: An indicator of how well the entity matches the query.

The id field refers to the MID (machine-generated identifier), a unique identifier for each entity.

This typically starts with kg:/m/ followed by a short appended string. MIDs break down human language into a format that machines can understand.

These MIDs also match the entity in Google Trends and can also be used to retrieve the URL of each entity, even if there is no knowledge panel for it.

Confidence Score

The resultScore (also known as confidence score) represents Google’s confidence in its understanding of the entity. It’s essentially the perceived strength of the relationship between the entity that Google has recognized for the query, and the entity that has been returned.

The higher the result score, the more confidence Google has in the entity being the best match for the query.

However, there is no guarantee that the entity with the highest score will appear as the featured snippet in the search results.

This score, particularly, is useful when analyzing different queries for opportunities.

For example, suppose you notice low constituency scores for a particular query. In that case, this highlights the opportunity to optimize pages to overtake the identified pages for the entity.

The URL for the entity is also considered the “entity home,” which is the website and page that Google recognizes as the most authoritative source with the most accurate information about the entity.

To improve the confidence score, ensuring that your website is consistent with the information on the entity’s home is important.

Improving the quality and detail provided on a webpage will also help improve the confidence score, pairing this with PR activity to further enhance the website’s authority for the chosen entity topic.

Extracting Insights

You can do several things with your Knowledge Graph response findings, including identifying areas of opportunity and reviewing current entities and entity homes for particular queries.

For example, ensuring you have the most appropriate schema markup and on-page optimization to connect with your target entities is an important first step.

Keyword Research

When completing keyword research, it is worth considering whether your current targeting makes sense if a strong entity exists for a particular keyword.

After all, Google’s overarching goal is to provide the most useful information in search results. With zero-click search increasing, competition for search terms and the ability to appear in SERP features is also increasing.

Brand Building

Using entities is an excellent way to build a brand or company’s organic search presence and authority in a particular space.

It’s useful to know the entities behind a certain query. They can give us insights around the search insight for keywords and make it even easier to create authoritative, helpful content in line with this.

Competitor Research

As the API provides a ranked list of entities that appear for queries, you can view a high level of insights rather than performing numerous searches to see what appears.

This will enable you to review your competitors’ performance for particular queries and how you compare.

You can also use these insights to ensure you can increase your confidence score to overtake competitors in the results.

The API allows you to keep track of this regularly and report on any changes you see, potentially before any SERP features change.

In Summary

I hope this has provided you a place to start with analyzing the Knowledge Graph and extracting valuable insights to help optimize your appearance in search features.

As Google explains, the Knowledge Graph is used to enhance Google search to find the right thing, get the best summary, and go deeper and broader.

Being able to see under the hood of the Knowledge Graph is a great place to start to ensure your website is the best source for Google to use to do just that.

I have created a Google Colab notebook here for you to use and play around with the code.

I’d love to know what insights you have extracted for your queries. (Please remember to make a copy and to add your own generated API).

You can also find a version of the code on GitHub here.

More resources:

How To Maximize Your Reach Using Google’s Knowledge Graph A Complete List Of The Different Types Of Featured Snippets How Search Engines Work

Featured Image: REDPIXEL.PL/Shutterstock