我可以返回输出,但不能使用python 3.x过滤返回数据
它返回一堆具有独特风格的结果,我只想从结果中获取htmlSpinnet和htmlTitle值
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)
它返回此错误
/Users/valizadavali/PycharmProjects/webScrap/venv/bin/python /Users/valizadavali/PycharmProjects/webScrap/googleCustomSearch.py
Traceback (most recent call last):
File "/Users/valizadavali/PycharmProjects/webScrap/googleCustomSearch.py", line 20, in <module>
for (key, value) in results.items():
AttributeError: 'list' object has no attribute 'items'
它不过滤就返回此值,我需要获取加粗的值
{'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':'2019年4月3日... PrezidentİlhamƏliyevməhkəmə-hüquq''sistemindəislahatlarındərinləşdirilməsi\ n''haqqındafərmanimzalayıb。','htmlTitle':'Məhkəmə-hüquqsistemindəislahatlardərinləşdirilir-','kind':'customsearch#result','link':'https://azvision.az/news/174505/mehkeme-huquq-sisteminde-islahatlar-derinlesdirilir--ferman--.html',}
...&fields=items(htmlTitle,htmlSnippet)...