python 面向对象入门 - 之 Web Service

本文介绍如何利用Python和SOAPpy库通过命令行来调用Google搜索API,包括安装必要的库、设置API密钥及具体实现代码。该示例展示了面向对象封装的方法,并提供了从Google搜索结果中提取标题、链接和描述的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

该博文涵盖了如下内容:
1. WSDL
2. SOAPpy
3.面向对象封装方法

#encoding=utf-8
"""
Search Google from the command line
url : http://pywebsvcs.sourceforge.net/
Install http://ncu.dl.sourceforge.net/project/pywebsvcs/SOAP.py/SOAPpy%200.11.0/SOAPpy-0.11.0.tar.gz
author : edison & dive in python
"""

from SOAPpy import WSDL

#see http://code.google.com/intl/zh-CN/apis/soapsearch/api_faq.html
WSDLFILE = 'http://api.google.com/GoogleSearch.wsdl'
APIKEY = 'key'

#建立web service
_server = WSDL.Proxy(WSDLFILE)
#打印soap输入,输出
_server.soapproxy.config.dumpSOAPOut = 1
_server.soapproxy.config.dumpSOAPIn = 1

def search(q):
print "print server methods"
print _server.methods.keys()

"""Search Google and return list of {title, link, description}"""
results = _server.doGoogleSearch(
APIKEY, q, 0, 10, False, "", False, "", "utf-8", "utf-8")
return [{"title": r.title.encode("utf-8"),
"link": r.URL.encode("utf-8"),
"description": r.snippet.encode("utf-8")}
for r in results.resultElements]

if __name__ == '__main__':
import sys
if sys.argv[1] :
for r in search(sys.argv[1])[:5]:
print r['title']
print r['link']
print r['description']
print

if __debug__:
#只打印前5条
for r in search("tl50")[:5]:
print r['title']
print r['link']
print r['description']
print
pass


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值