Python装饰器使用例子 网页爬取测速

装饰器的主要作用是,在不修改其他函数的前提下,为被封装函数添加代码,装饰器语法优美,big很高
python装饰器一个使用的例子,主要功能是爬取网页,然后计算爬了多长时间

#无参装饰器

#!/bin/python3
# -*- coding:utf-8 -*-
''' python no parameter decorator example  '''
#modules
import requests
import time
#functions
def timer(func):
    def wrappers():
        start_time = time.time()
        res = func()
        end_time = time.time()
        return res,end_time - start_time
    return wrappers

@timer
def get_url():
    print("crawing...")
    html_status = requests.get("https://www.slll.info")
    return html_status
def main():
    res = get_url()
    print("web status: %s , cost time: %.2f/s" %(res[0],res[1]))
#main program
if __name__ == "__main__":
    main()

#有参装饰器

#!/bin/python3
# -*- coding:utf-8 -*-
''' python parameter decorator example  '''
#modules
import requests
import time
#functions
def timer(func):
    def wrappers(*args,**kwargs):
        temp_list = list(args[0]) #tuple -> list
        count = 0 #define index cout
        print("crawing...")
        for x in temp_list:
            start_time = time.time() #start time count
            html_status = func(x['url']) #get html status code
            print("%s ... done." %x['url']) #get action dond tips
            end_time = time.time() #stop time count
            #write to dict
            temp_list[count]['statuscode'] = '%s' %html_status #change url status code
            temp_list[count]['costtime'] = '%.2f/s' %float(end_time - start_time) #change url costtime
            #count add and goto next loop
            count += 1
        return temp_list
    return wrappers

@timer #call decorator
def get_url(url):
    ''' url(str) -> url.status_code '''
    html_status = requests.get(url)
    return html_status.status_code
def main():
    ''' main program '''
    url_list = [
        {"url":"https://www.baidu.com",'statuscode':"","costtime":""},
        {"url":"https://www.weibo.com",'statuscode':"","costtime":""},
        {"url":"http://www.yahoo.com",'statuscode':"","costtime":""},
        {"url":"http://music.163.com",'statuscode':"","costtime":""},
    ]
    res = get_url(url_list)
    #print test list
    print("\n------website-get-speed-test------")
    print("---url-----status-----cost-time---")
    for x in res:
        print("[%s]-[%s]-[%s]" %(x['url'],x['statuscode'],x['costtime']))
#program entry
if __name__ == "__main__":
    main()

Leave a Reply

Your email address will not be published. Required fields are marked *