標籤:
import httplib2,time
#裝飾器方法,用於記錄方法消耗時間
#推薦將print 改成log
def timer(func):
def _warpper(self,*argv):
start = time.time()
result = func(self,*argv)
cost = time.time() - start
print ‘The function %s coust time %f sec‘ % (func.func_name,cost)
return result
return _warpper
class Spider(object):
"""docstring for Spider"""
def __init__(self):
super(Spider, self).__init__()
self.h = httplib2.Http(‘.cache‘)
@timer
def httpGet(self,urlstr,word=""):
#httplib2.debuglevel = 1
urlstr = urlstr
self.head,self.content = self.h.request(urlstr)
#print(content)
@timer
def httpPost(self,urlstr,data):
from urllib import urlencode
self.head,self.content = self.h.request(urlstr, ‘POST‘, urlencode(data), headers={‘Content-Type‘: ‘application/x-www-form-urlencoded‘})
def getContent(self):
return self.content.decode(‘utf-8‘)
def getResponse(self):
return self.head
- #post傳參資料
data = {‘password‘:‘s2105535‘,‘submit‘:‘Login‘,‘username‘:‘qq64397232‘}
spider = Spider()
spider.httpPost(
‘http:/12121212.com‘
,data)
來自為知筆記(Wiz)
python httplib2應用get post