处理post请求,如果传data,则为post请求
import urllib
from urllib.request import Request
from urllib.parse import urlencode
url = 'http://www.xxxx.com/login'
data = {
'username': 'admin', 'password': abcde}
data = urlencode(data) # 将字典类型的请求数据转成url编码
data = data.encode('ascii') # 将url编码类型的请求数据转为bytes类型
req_data = Request(url, data) # 将url和请求数据处理为一个Request对象,供urlopen调用
with urlopen(req_data) as res:
res = res.read().decode() # read()方法是读取返回数据内容,decode是转换返回数据的bytes格式为str
print(res)
Python3.x常用时间的处理方法
from datetime import date
today = datetime.date.today() # 今天
yesterday = today - datetime.timedelta(days=1) # 昨天, 要是前天,把days赋值2
last_month = today.month - 1 if today.month - 1 else 12 # 上个月
time_stamp = time.time() # 当前的时间戳
datetime.datetime.fromtimestamp(time_stamp) # 时间戳转datetime
int(time.mktime(today.timetuple())) # datetime转时间戳
today_str = today.strftime('%Y-%m-%d') # datetime转换字符串
today = datetime.datetime.strptime(today_str, '%Y-%m-%d') # 字符串转datetime
today + datetime.timedelta(hours = 8) # 补时差