来自 Python 应用程序的 Google 搜索 [英] Google Search from a Python App

查看:22
本文介绍了来自 Python 应用程序的 Google 搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 python 应用程序运行谷歌搜索查询.是否有任何 python 接口可以让我这样做?如果没有人知道哪个 Google API 可以让我做到这一点.谢谢.

I'm trying to run a google search query from a python app. Is there any python interface out there that would let me do this? If there isn't does anyone know which Google API will enable me to do this. Thanks.

推荐答案

有一个简单的例子 here (特别是缺少一些引号;-).您将在 Web 上看到的大部分内容是旧的、已停产的 SOAP API 的 Python 接口——我所指的示例使用了较新且受支持的 AJAX API,这绝对是您想要的!-)

There's a simple example here (peculiarly missing some quotes;-). Most of what you'll see on the web is Python interfaces to the old, discontinued SOAP API -- the example I'm pointing to uses the newer and supported AJAX API, that's definitely the one you want!-)

编辑:这是一个更完整的 Python 2.6 示例,其中包含所有需要的引号 &c;-)...:

Edit: here's a more complete Python 2.6 example with all the needed quotes &c;-)...:

#!/usr/bin/python
import json
import urllib

def showsome(searchfor):
  query = urllib.urlencode({'q': searchfor})
  url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % query
  search_response = urllib.urlopen(url)
  search_results = search_response.read()
  results = json.loads(search_results)
  data = results['responseData']
  print 'Total results: %s' % data['cursor']['estimatedResultCount']
  hits = data['results']
  print 'Top %d hits:' % len(hits)
  for h in hits: print ' ', h['url']
  print 'For more results, see %s' % data['cursor']['moreResultsUrl']

showsome('ermanno olmi')

这篇关于来自 Python 应用程序的 Google 搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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