我可以返回输出,但不能使用python3.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', '显示链接':'azvision.az公司', '格式化URL':'https://azvision.az/.../mehkeme-huquq-sisteminde-islahatlar-derinlesdirilir--' '费曼--.html', 'htmlFormattedUrl':'https://azvision.az/.../mehkeme-huquq-sisteminde-islahatlar-derinlesdirilir--' '费曼--.html', “htmlSnippet”:2019年4月3日..PrezidentİlhamƏliyev məhkəmə-hüqu' 'sistemindəislahatların dərinləşdirilməsiəbr/>\n' “haqqında fərman imzalayıb.”, “htmlTitle”:“MəhkəMə-hüqu sistemindəislahatlar dərinləşdirilir-”, 'kind':'customsearch#result', “链接”:“https://azvision.az/news/174505/mehkeme-huquq-sisteminde-islahatlar-derinlesdirilir--ferman--.html”, }你知道吗
您也可以通过使用“fields”参数将您对google的请求更改为专门请求“htmlSnippet”和“htmlTitle”:
这会使返回的结果更容易解析吗?你知道吗
有关详细信息,请参见此链接:fields
相关问题 更多 >
编程相关推荐