1. 程式人生 > >python模塊整理

python模塊整理

pre 函數 rom 單位 一個 cnblogs 地址 ont 閉包

一、time模塊:import time

  1、time.sleep(3) #單位為妙

二、urllib

  1、傳遞一個url地址得到其html內容:from urllib.request import urlopen

from  urllib.request  import urlopen
res = urlopen(‘http://crm.oldboyedu.com‘).read()   #打印出的是16進制
print(res.decode(‘utf-8‘))     #  轉換為漢字

def index(url):
    def get():
        old_boy= urlopen(url).read()
        return old_boy
    return get

old_boy = index(‘http://crm.oldboyedu.com‘)  
print(old_boy.__closure__[0].cell_contents)   #每一個函數都有一個__closure__函數,如果是閉包函數,則其cell_contents方法返回閉包的值:http://crm.oldboyedu.com
print(old_boy().decode(‘utf-8‘))

 

python模塊整理