- 论坛徽章:
- 0
|
昨天突然想到用google查询点东西,后来想起了O'Reilly 的google hacks这本书,
翻了一下,看到可以用python实现,就试了一试.注意要想通过python使用google api,那么需要SOAPPY,还有ZSI,pygoogle等模块.另外,要安装python的模块,还需要用到python-setuptools.
源代码如下:
#!/usr/bin/python
import sys,string,codecs
#google search api
import google
#handling command line arguments
import getopt
if sys.argv[1:]:
#如果不进行unicode转换,好像查中文时会出错
query=unicode(sys.argv[1],'utf-8')
else:
sys.exit('Usage: python gsearch.py ')
#my google license key
google.LICENSE_KEY='输入你的google license key'
#query google
data=google.doGoogleSearch(query)
sys.stdout=codecs.lookup('utf-8')[-1](sys.stdout)
for result in data.results:
print string.join((result.title,result.URL,result.snippet),"\n"),"\n"
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/17260/showart_98066.html |
|