Python应用程序中的Google搜索

lmvvr0a8  于 2023-06-04  发布在  Python
关注(0)|答案(4)|浏览(478)

我在用python程序运行google搜索。有没有python接口可以让我这么做?如果没有,谁知道哪个Google API可以让我做到这一点。谢谢

2jcobegt

2jcobegt1#

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

编辑:这里有一个更完整的Python 2.6示例,包含所有需要的引号&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')
i86rm4rw

i86rm4rw2#

以下是Alex的答案,已移植到Python3

#!/usr/bin/python3
import json
import urllib.request, urllib.parse

def showsome(searchfor):
  query = urllib.parse.urlencode({'q': searchfor})
  url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % query
  search_response = urllib.request.urlopen(url)
  search_results = search_response.read().decode("utf8")
  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')
v8wbuo2f

v8wbuo2f3#

以下是我的方法:http://breakingcode.wordpress.com/2010/06/29/google-search-python/
下面是一些代码示例:

# Get the first 20 hits for: "Breaking Code" WordPress blog
    from google import search
    for url in search('"Breaking Code" WordPress blog', stop=20):
        print(url)

    # Get the first 20 hits for "Mariposa botnet" in Google Spain
    from google import search
    for url in search('Mariposa botnet', tld='es', lang='es', stop=20):
        print(url)

请注意,此代码不使用Google API,并且到目前为止(2012年1月)仍在工作。

bvjxkvbb

bvjxkvbb4#

我是Python新手,我正在研究如何做到这一点。没有一个例子对我来说是正确的。如果你提出很多(很少)请求,有些会被谷歌屏蔽,有些已经过时了。解析google搜索html(在请求中添加头部)将工作,直到google再次更改html结构。您可以使用相同的逻辑在任何其他搜索引擎中搜索,查看html(查看源代码)。

import urllib2

def getgoogleurl(search,siteurl=False):
    if siteurl==False:
        return 'http://www.google.com/search?q='+urllib2.quote(search)
    else:
        return 'http://www.google.com/search?q=site:'+urllib2.quote(siteurl)+'%20'+urllib2.quote(search)

def getgooglelinks(search,siteurl=False):
   #google returns 403 without user agent
   headers = {'User-agent':'Mozilla/11.0'}
   req = urllib2.Request(getgoogleurl(search,siteurl),None,headers)
   site = urllib2.urlopen(req)
   data = site.read()
   site.close()

   #no beatifulsoup because google html is generated with javascript
   start = data.find('<div id="res">')
   end = data.find('<div id="foot">')
   if data[start:end]=='':
      #error, no links to find
      return False
   else:
      links =[]
      data = data[start:end]
      start = 0
      end = 0        
      while start>-1 and end>-1:
          #get only results of the provided site
          if siteurl==False:
            start = data.find('<a href="/url?q=')
          else:
            start = data.find('<a href="/url?q='+str(siteurl))
          data = data[start+len('<a href="/url?q='):]
          end = data.find('&amp;sa=U&amp;ei=')
          if start>-1 and end>-1: 
              link =  urllib2.unquote(data[0:end])
              data = data[end:len(data)]
              if link.find('http')==0:
                  links.append(link)
      return links

用途:

links = getgooglelinks('python','http://www.stackoverflow.com/')
for link in links:
       print link

(Edit 1:添加一个参数来缩小谷歌搜索到一个特定的网站)
(Edit 2:当我添加这个答案时,我正在编写一个Python脚本来搜索字幕。我最近把它上传到Github:Subseek

相关问题