Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我想在我的自定义谷歌搜索引擎中搜索一个特定的单词,然后过滤该搜索的输出以获得标题_Python_Python 3.x_Google Custom Search - Fatal编程技术网

Python 我想在我的自定义谷歌搜索引擎中搜索一个特定的单词,然后过滤该搜索的输出以获得标题

Python 我想在我的自定义谷歌搜索引擎中搜索一个特定的单词,然后过滤该搜索的输出以获得标题,python,python-3.x,google-custom-search,Python,Python 3.x,Google Custom Search,我可以返回输出,但不能使用Python3.x过滤返回数据 它返回一组具有唯一样式的结果,我只想从结果中获取htmlSpinnet和htmlTitle值 从GoogleAppClient.discovery导入生成 导入pprint 我的api密钥=xxx my_cse_id=xxx 定义谷歌搜索术语、api密钥、cse id、**kwargs: service=buildcustomsearch,v1,developerKey=api\u键 res=service.cse.listq=searc

我可以返回输出,但不能使用Python3.x过滤返回数据

它返回一组具有唯一样式的结果,我只想从结果中获取htmlSpinnet和htmlTitle值

从GoogleAppClient.discovery导入生成 导入pprint 我的api密钥=xxx my_cse_id=xxx 定义谷歌搜索术语、api密钥、cse id、**kwargs: service=buildcustomsearch,v1,developerKey=api\u键 res=service.cse.listq=search\u term,cx=cse\u id,**kwargs.execute 返回res['items'] 结果=谷歌搜索 “mehkeme”,我的api密钥,我的cse id,num=10 这是htmlSpinnets,也是HtmlTile newDict=dict 迭代字典中的所有项,并筛选具有偶数键的项 对于键,results.items中的值: 如果键=='htmlSpinnet': newDict[键]=值 打印“过滤字典:” 印刷新词 对于结果中的结果: pprint.pprintresult 它返回这个错误

/Users/valizadavali/PycharmProjects/webScrape/venv/bin/python/Users/valizadavali/PycharmProjects/webScrape/googleCustomSearch.py 回溯最近一次呼叫上次: 文件/Users/valizadavali/PycharmProjects/webScrape/googleCustomSearch.py,第20行,在 对于键,results.items中的值: AttributeError:“列表”对象没有属性“项” 它返回这个值而不进行过滤,我需要得到粗体的值

{'cacheId':'fGQCNF9pc6cJ', “displayLink”:“azvision.az”, '格式化URL':'https://azvision.az/.../mehkeme-huquq-sisteminde-islahatlar-derinlesdirilir- ' “ferman-.html”, “htmlFormattedUrl”:”https://azvision.az/.../mehkeme-huquq-sisteminde-islahatlar-derinlesdirilir- ' “ferman-.html”, ‘HTMLSNIPET’:‘2019年4月3日……PrezidentİlhamƏliyev məhkəmə-hüqu’ “islahatlar n dərinlədirilməsi\n姐妹会” “haqqında fərman imzalayıb.”, “Htmltile”:MəhkəMə-hüqu sistemindəislahatlar dərinləşdirilir-”, “种类”:“customsearchresult”, “链接”:https://azvision.az/news/174505/mehkeme-huquq-sisteminde-islahatlar-derinlesdirilir-ferman-.html',
}

您也可以通过使用“字段”参数,将您对谷歌的请求更改为专门请求“htmlSnippet”和“htmlTitle”:

...&fields=items(htmlTitle,htmlSnippet)...
这会使返回的结果更容易解析吗


有关更多信息,请参阅此链接:

Dict类来自何处?你进口了吗?如果您从其他地方复制此代码,是否可以提供链接?我更新代码并查看您是否尝试替换结果中的键和值。项:对于键,结果中的值[0]。项:?哦,糟糕,替换为键,结果中的值。项:对于键,结果中的值。项:删除结果中的s是非常简单,你可以在这里看到一些例子,我将尝试一下,你能看看我的未回答的问题吗?我不是很精通python,所以不能真正帮助你,我只是发现你只需要搜索结果中的两个属性,所以我建议了我们在生产中广泛使用的解决方案,效果非常好。无论如何,谢谢,搜索多个关键字怎么样,比如我们正在创建搜索列表,对于每一个关键字,它都会将每次搜索的结果作为列表的命令返回。你明白我的意思了吗?没有完全明白,如果你有多个单词,那么也许你可以对每个关键字运行一个api查询,然后将结果聚合到一个字典中?