Thursday, October 24, 2024

Python Code for Google Search Autocomplete Suggestions

import requests

headers = {
    'accept': '*/*',
    'accept-language': 'en-US,en;q=0.9,de;q=0.8,ro;q=0.7',
    'cache-control': 'no-cache',
    'pragma': 'no-cache',
    'priority': 'u=1, i',
    'referer': 'https://www.google.com/',
    'sec-ch-prefers-color-scheme': 'light',
    'sec-ch-ua': '"Google Chrome";v="129", "Not=A?Brand";v="8", "Chromium";v="129"',
    'sec-ch-ua-arch': '""',
    'sec-ch-ua-bitness': '"64"',
    'sec-ch-ua-form-factors': '',
    'sec-ch-ua-full-version': '"129.0.6668.71"',
    'sec-ch-ua-full-version-list': '"Google Chrome";v="129.0.6668.71", "Not=A?Brand";v="8.0.0.0", "Chromium";v="129.0.6668.71"',
    'sec-ch-ua-mobile': '?1',
    'sec-ch-ua-model': '"Nexus 5"',
    'sec-ch-ua-platform': '"Android"',
    'sec-ch-ua-platform-version': '"6.0"',
    'sec-ch-ua-wow64': '?1',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-origin',
    'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Mobile Safari/537.36',
}

params = {
    'q': 'Tiberiu Cristian Leon',
    'cp': '10',
    # 'client': 'mobile-gws-wiz-serp',
    'client': 'chrome',
    'hl': 'en-RO',
    'authuser': '0',
}

response = requests.get('http://google.com/complete/search', headers=headers, params=params)
print(', '.join(response.json()[1]))