使用python脚本进行谷歌搜索
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3898574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google search using python script
提问by sudh
Could anyone help me on how to write a python script that searches google and prints the links of top results.
任何人都可以帮助我编写一个 python 脚本来搜索谷歌并打印顶级结果的链接。
采纳答案by LK-
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.
回答by Shiv Deepak
it is better suggested to use google apis but a very ugly version.. (alternative to use google api) you can filter content if you want
最好建议使用 google apis 但一个非常丑陋的版本..(替代使用 google api)你可以根据需要过滤内容
import os, urllib, sys
filename = 'http://www.google.com/search?' + urllib.urlencode({'q': ' '.join(sys.argv[1:]) })
cmd = os.popen("lynx -dump %s" % filename)
output = cmd.read()
cmd.close()
print output
it will print exactly what ever a browser should display when you search for something on google
当您在 google 上搜索内容时,它将准确打印浏览器应显示的内容
回答by Ronis Gracie
from pygoogle import pygoogle
g = pygoogle('quake 3 arena')
g.pages = 5
print '*Found %s results*'%(g.get_result_count())
g.get_urls()
回答by L?iten
As @Zloy Smiertniy pointed out, the answer can be found here.
正如@Zloy Smiertniy 指出的,答案可以在这里找到。
However, if you are using Python 3 the syntax of raw_input, urllibhas changed, and one has to decode the response. Thus, for Python 3 one can use:
但是,如果您使用的是 Python 3,raw_input,的语法urllib已更改,并且必须解码response. 因此,对于 Python 3 可以使用:
import urllib
import urllib.request
import json
url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&"
query = input("Query:")
query = urllib.parse.urlencode( {'q' : query } )
response = urllib.request.urlopen (url + query ).read()
data = json.loads ( response.decode() )
results = data [ 'responseData' ] [ 'results' ]
for result in results:
title = result['title']
url = result['url']
print ( title + '; ' + url )
回答by Rohith Sankar
I'm a newbie to Python. Just my simple idea for a google search.
我是 Python 的新手。只是我对谷歌搜索的简单想法。
import webbrowser
lib=raw_input("Enter what you want to search for:")
ur="https://www.google.co.in/gfe_rd=cr&ei=Q7nZVqSBIMSL8QeBpbOoDQ#q="
webbrowser.open_new(ur+lib)
回答by Mansoor Akram
Try this, its very simple to use: https://pypi.python.org/pypi/google
试试这个,使用起来非常简单:https: //pypi.python.org/pypi/google
Docs: https://breakingcode.wordpress.com/2010/06/29/google-search-python/
文档:https: //breakcode.wordpress.com/2010/06/29/google-search-python/
Github: https://github.com/MarioVilas/google
Github:https: //github.com/MarioVilas/google
Install this python package and usage is as simple as this:
安装这个python包,使用就这么简单:
# Get the first 5 hits for "google 1.9.1 python" in Google Pakistan
from google import search
for url in search('google 1.9.1 python', tld='com.pk', lang='es', stop=5):
print(url)
回答by mayank
Try the following:
请尝试以下操作:
import webbrowser
lib = input()
url = "https://www.google.co.in/search?q=" +(str(lib))+ "&oq="+(str(lib))+"&gs_l=serp.12..0i71l8.0.0.0.6391.0.0.0.0.0.0.0.0..0.0....0...1c..64.serp..0.0.0.UiQhpfaBsuU"
webbrowser.open_new(url)
回答by lf2225
I've used SERP API to accomplish this.
我已经使用 SERP API 来实现这一点。
The instructions are fairly simple:
说明相当简单:
pip install google-search-results
and the usage is:
用法是:
from lib.google_search_results import GoogleSearchResults
query = GoogleSearchResults({"q": "coffee"})
json_results = query.get_json()
More advanced uses are on Github.
更高级的用法在 Github 上。

