I can return the output but can not filter the return data using python 3.x
it returns a bunch of result with a unique style and i just only want to get htmlSpinnet and htmlTitle values from the result
from googleapiclient.discovery import build
import pprint
my_api_key = "xxx"
my_cse_id = "xxx"
def google_search(search_term, api_key, cse_id, **kwargs):
service = build("customsearch", "v1", developerKey=api_key)
res = service.cse().list(q=search_term, cx=cse_id, **kwargs).execute()
return res['items']
results = google_search(
'mehkeme', my_api_key, my_cse_id, num=10)
# this is the htmlSpinnets and also htmlTitle
newDict = dict()
# Iterate over all the items in dictionary and filter items which has even keys
for (key, value) in results.items():
if key == 'htmlSpinnet':
newDict[key] = value
print('Filtered Dictionary : ')
print(newDict)
# for result in results:
# pprint.pprint(result)
it returns this error
/Users/valizadavali/PycharmProjects/webScrape/venv/bin/python /Users/valizadavali/PycharmProjects/webScrape/googleCustomSearch.py
Traceback (most recent call last):
File "/Users/valizadavali/PycharmProjects/webScrape/googleCustomSearch.py", line 20, in <module>
for (key, value) in results.items():
AttributeError: 'list' object has no attribute 'items'
it returns this without filtering, and I need to get the values which are bolded
{'cacheId': 'fGQCNF9pc6cJ',
'displayLink': 'azvision.az',
'formattedUrl': 'https://azvision.az/.../mehkeme-huquq-sisteminde-islahatlar-derinlesdirilir-- '
'ferman--.html',
'htmlFormattedUrl': 'https://azvision.az/.../mehkeme-huquq-sisteminde-islahatlar-derinlesdirilir-- '
'ferman--.html',
'htmlSnippet': '3 Apr 2019 ... Prezident İlham Əliyev məhkəmə-hüquq '
'sistemində islahatların dərinləşdirilməsi
\n'
'haqqında fərman imzalayıb.',
'htmlTitle': 'Məhkəmə-hüquq sistemində islahatlar dərinləşdirilir -',
'kind': 'customsearch#result',
'link': 'https://azvision.az/news/174505/mehkeme-huquq-sisteminde-islahatlar-derinlesdirilir--ferman--.html',
}