Open sq-yuan opened 1 year ago
answer_box.type
also depends the SERP locale.
@sq-yuan Since we haven't provided you with an explicit solution, here's one way of doing it by mapping people_also_search
key and explicitly writing how people_also_search
will be named in other languages (people_also_search_for_key
).
Python code example in the online IDE with explanation:
from serpapi import GoogleSearch
import os
# creating a tuple or list or any data structure of places we want to extract data from
locations = ('France', 'United States', 'Germany', 'Poland', 'Japan', 'Italy')
# mapping those places since this data won't be changed
# telling how the 'people_also_search_for' hash key will be named in other languages: 'people_also_search_for_key'
location_mappings = {
'France': {'gl': 'fr', 'hl': 'fr', 'google_domain': 'google.fr', 'people_also_search_for_key': 'recherches_associées'},
'United States': {'gl': 'us', 'hl': 'en', 'google_domain': 'google.com', 'people_also_search_for_key': 'people_also_search_for'},
'Germany': {'gl': 'de', 'hl': 'de', 'google_domain': 'google.de', 'people_also_search_for_key': 'andere_suchten_auch_nach'},
'Poland': {'gl': 'pl', 'hl': 'pl', 'google_domain': 'google.pl', 'people_also_search_for_key': 'podobne_wyszukiwania'},
'Japan': {'gl': 'jp', 'hl': 'ja', 'google_domain': 'google.co.jp', 'people_also_search_for_key': '他の人はこちらも検索'},
'Italy': {'gl': 'it', 'hl': 'it', 'google_domain': 'google.it', 'people_also_search_for_key': 'ricerche_correlate'}
}
# iterating over locations tuple
# passing mapping data to 'google_domain', 'gl', 'hl', 'location' depending on 'location' (for loop variable)
for location in locations:
params = {
'engine': 'google',
'no_cache': 'true',
'q': 'apple',
'google_domain': location_mappings[location]['google_domain'],
'device': 'desktop',
# 'kgmid': '/m/0k8z', # https://serpapi.com/search-api#api-parameters-advanced-google-parameters-kgmid
'hl': location_mappings[location]['hl'],
'gl': location_mappings[location]['gl'],
'location': location,
'api_key': os.getenv('API_KEY') # change to your API key
}
search = GoogleSearch(params)
results = search.get_dict()
# access 'people_also_search_for_key' and store it to a variable
# 'get()' 'people_also_search_for_key' ('people_also_search_for' translated to other languages)
# from 'knowledge_graph' or return an empty list if nothing found
people_also_search_for_key = location_mappings[location]['people_also_search_for_key']
people_also_search_for = results['knowledge_graph'].get(people_also_search_for_key, [])
print(f'{location}: {people_also_search_for}', sep='\n')
Let me know if it helped or not 🙂
When searching Knowledge Graph, the "people also search for section" result uses different keys for different languages.
In en-US, it's "people_also_search_for", in fr-FR, it's "recherches_associées", it'd be great to have a single name, so we can using the same code to handling results from different languages.
![Screen Shot 2022-10-03 at 11 23 35 AM](https://user-images.githubusercontent.com/4009348/193651609-1cfe04a3-a087-44f5-afbc-dfd0a8b8dcf2.png)