from datetime import datetime
print('获取当前时间:')
now = datetime.now()
print(now)
print(type(now))
print('获取指定日期和时间:')
dt = datetime(2015, 4, 19, 20, 20)
print(dt)
from collections import deque
q=deque(['a','b','c'])
q.append('x')
q.appendleft('y')
print(q)
print('counter 简单计数:')
from collections import Counter
c=Counter()
for ch in 'programming':
c[ch]=c[ch]+1
print(c)
print('内置base64进行二进制编码:')
import base64
a=base64.b64encode(b'binary\x00string')
print(a)
print('hashlib计算摘要:')
import hashlib
md5=hashlib.md5()
md5.update('how to use md5 in python hashlib?'.encode('utf-8'))
print(md5.hexdigest())
import hashlib
md5 = hashlib.md5()
md5.update('how to use mds in'.encode('utf-8'))
md5.update('python hashlib?'.encode('utf-8'))
print(md5.hexdigest())
print('无限迭代器 ctrl+c退出:')
import itertools
ns=itertools.repeat('A', 3)
for n in ns:
print(n)
print('模拟iphone6去登录豆瓣首页:')
from urllib import request
req = request.Request('http://www.douban.com/')
req.add_header('User-Agent',
'Mozilla/6.0(iPhone;CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 '
'(KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25')
with request.urlopen(req) as f:
print('Status:',f.status,f.reason)
for k,v in f.getheaders():
print('%s:%s' % (k, v))
print('Data:', f.read().decode('utf-8'))
print('使用requests')
import requests
r=requests.get('https://www.douban.com/')
print(r.status_code)
print(r.text)
r = requests.get('https://www.douban.com/search', params={'q': 'python', 'cat': '1001'})
print(r.url)
print(r.content)