wrong number of results from google search

21 Views Asked by At

I have a problem with a difference between number of results from my code and directly from google search.

My code showes me for example i have 334 000 000 results for 'Python programming language', but when I typed the same phrase to google, I receive 680 000 000.

import requests

def count_google_results(query, api_key, cx):
    try:
        params = {
            'key': api_key,
            'cx': cx,
            'q': query,
            'gl': 'PL',  # Set country code to Poland
            'hl': 'pl'   # Set language to Polish
        }
        url = "https://www.googleapis.com/customsearch/v1"
        response = requests.get(url, params=params)
        data = response.json()
        total_results = data['searchInformation']['totalResults']
        print(f"Number of results for query '{query}' is: {total_results}")
    except Exception as e:
        print("An error occurred:", e)

query = "Python programming language"
api_key = "YOUR_API_KEY"
cx = "YOUR_CUSTOM_SEARCH_ENGINE_ID"
count_google_results(query, api_key, cx)

I used the code above with my api key and custom search engine id.

It's one of my first projects and I don't know what is wrong and how to correct that.

Chat gpt 3 didn't help me with finding a solution (he suggested adding some searching params like PL, because I am in Poland now, but it didn't help - number of results is different (294 000 000), but still not the same as in reality - 680 000 000).

0

There are 0 best solutions below