从Python应用程序谷歌搜索 [英] Google Search from a Python App

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

问题描述

我试图运行Python应用程序谷歌搜索查询。是否有任何Python接口在那里它可以让我做到这一点?如果没有没有人知道哪些谷歌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.

推荐答案

有一个简单的例子,<一个href=\"http://www.ajaxlines.com/ajax/stuff/article/%5Fgoogle%5Fajax%5Fsearch%5Fapi%5Fexample%5Fpython%5F$c$c.php\">here (缺少特有的一些报价;-)。大多数的什么,你会在网络上看到的是Python的接口,旧的,停产SOAP API - 我指着示例使用了更新,支持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应用程序谷歌搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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