使用python脚本进行谷歌搜索 [英] Google search using python script

查看:53
本文介绍了使用python脚本进行谷歌搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能帮我写一个python脚本来搜索谷歌并打印热门结果的链接.

解决方案

也许是这样?

<前>导入 urllib将 json 导入为 m_json查询 = 原始输入('查询:')查询 = urllib.urlencode ( { 'q' : 查询 } )response = urllib.urlopen ( 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&' + query ).read()json = m_json.loads(响应)结果 = json ['responseData'] ['结果']结果结果:标题 = 结果['标题']url = result['url'] # 是原来的 URL 并且抛出了名称错误异常打印(标题 + '; ' + url )

阅读文档http://docs.python.org/

由于 AJAX API 已死,您可以使用第三方服务,例如 SerpApi,他们可以提供 Python 库.

Could anyone help me on how to write a python script that searches google and prints the links of top results.

解决方案

Maybe, something like this?

import urllib
import json as m_json
query = raw_input ( 'Query: ' )
query = urllib.urlencode ( { 'q' : query } )
response = urllib.urlopen ( 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&' + query ).read()
json = m_json.loads ( response )
results = json [ 'responseData' ] [ 'results' ]
for result in results:
    title = result['title']
    url = result['url']   # was URL in the original and that threw a name error exception
    print ( title + '; ' + url )

Read the docs http://docs.python.org/

[Edit] As the AJAX API is dead, you can use a third party service, like SerpApi, they do provide a Python library.

这篇关于使用python脚本进行谷歌搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆