time/requests/hashlib/hmac/re模块

time module

It offers three different types of time (time stamp)

import time 
 # 时间戳:时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。
print(time.time()) 


 # 格式化时间
print(time.strftime('%Y-%m-%d  %X')) # 获取当前时间,'-'为拼接符

 # 结构化时间
print(time.localtime)



 # (****)
 # 只要记住time.time()和time.sleep(1)

datetime module

Time of addition and subtraction

import datetime

now = datetime.datetime.now()   # 获取当前时间
print(now)


print(now + datetime.timedelta(3))  # 默认加天3天

print(now + datetime.timedelta(weeks=3))    # 加3周

print(now + datetime.timedelta(hours=3))    # 加3小时  

print(now - datetime.timedelta(hours=3))    # 减

print(now + datetime.timedelta(hours=-3))   # 减

now.replace(year=1949, month=10, day=1, hour=10, minute=1, second=0, microsecond=0) 

random module

random number

grasp

print(random.random())  # 0-1随机

print(random.randint(1,3))  # 1-3整数


lt= [1,2,3,4,5]
print(random.shuffle(lt))   # 打乱列表中的顺序

print(random.choice(lt)     # 随机选择一个

random.seed(1)
print(random.choice(lt))    # 只随机一次

To understanding

print(random.sample([1,23,4],2))    # 随机生成2个数

hashlib and hmac module

# 叠加性
m = hashlib.md5()
# m.update(b'say')
# m.update(b'hello')
m.update(b'sathello')

print(m.hexhdigest())   # 对于不同的字符而言,永不重复

hash_pwd = '0562b36c3c5a3925dbe3c4d32a4f2ba2'


pwd_list = [
    'hash3714',
    'hash1313',
    'hash94139413',
    'hash123456',
    '123456hash',
    'h123ash',
]


for pwd in pwd_list:
    m = hashlib.md5()
    m.update(pwd.encode('utf-8'))
    res = m.hexdigest()
    if res == hash_pwd
        print(f'{pwd}')
        
 # hmac密钥
import hmac
m = hmac.new(b'make')   #密钥
m.update(b'hash123456)
print(m.hexdigest())
         
m = hmac.new(b'sssfe')  
m.update(b'hash123456)
print(m.hexdigest())


         

typing module

A function associated with the control function parameter data type, a data type other than the type of underlying data

lt = [1,2,3,4]
print()

from typing import Iterable
def func(x:int lt:Iterable) ->list
    return [1,2,3]

requester module

Reptiles - "crawl data, analog's browser sends a request to the url, get data

import requests

response = requests.get('http://duanziwang.com/')
data = response.txt
print(data)

re module

String to find String meet certain characteristics

^: ... begins with

import re

s = 'abcdabc'
res = re.findall('^ab',s)       
 # ['ab','ab']

$: Ends with ...

s = 'abcdabcall'
res = re.findall('all$',s)      
 # ['all']

: Any character *

s = 'abcdabcp'
res = re.findall('ab.',s)       
 # ['abc','abp']

\ D: Digital *

s = 'abcd1ab2p14'
res = re.findall('\d',s)        
 # ['1','2','1','4']

\ D: non-numeric *

s = 'abcd1ab2p14'
res = re.findall('\w',s)        
 # ['a', 'b', 'c', 'd', '1', 'a', 'b', '2', 'p', '1', '4']

\ W: non-empty *

s = 'abcd1ab2p14' #字母数字下划线等
res = re.findall('\w',s)        
 # ['a', 'b', 'c', 'd', '1', 'a', 'b', '2', 'p', '1', '4']

\ S: empty

s = 'abcd1 ab2p14'
res = re.findall('\s',s)        
 # ['']

\ S: non-empty *

s = 'abcd1 ab2p14'
res = re.findall('\s',s)        
 # ['a', 'b', 'c', 'd', '1', 'a', 'b', '2', 'p', '1', '4']

+: In front of at least one character *

s = 'abcdddd abcd abc'
res = re.findall('abcd+',s)     
 # ['abcdddd', 'abcd']

? : Previous character to 0-1 *

s = 'abcdddd abcd abc'
res = re.findall('abcd?',s)     
 # ['abcd', 'abcd','abc']

*: In front of the character to 0 *

s = 'abcdddd abcd abc'
res = re.findall('abcd*',s)     
 # ['abcd', 'abcd','abc']

[]: Can (only one character) in brackets

s = 'abc bbc cbc dbc'
print(re.findall('[abc]bc',s))
 # ['abc', 'bbc', 'cbc']

[^]: Not possible (only one character) in brackets

s = 'abc bbc cbc dbc'
print(re.findall('[^abc]bc',s))
 # ['dbc']

|: Or *

s = 'abc bbc cbc dbc'
print(re.findall('abc|bbc',s))
 # ['dbc','bbc']

Greedy *

s = 'abcdefgaaaaaaaaaag'
print(re.findall('a.*g',s))
 # ['abcdefgaaaaaaaaaag'] 最大跨度 找到最后一个

Non-greedy mode *

s = 'abcdefgaaaaaaaaaag'
print(re.findall('a.*?g',s))
 # ['abcdefg', 'aaaaaaaaaag'] 小跨度

function

compile


s = 'abcd abcddd abc'
res = re.compile('abcd*',s)
phone_patter = re.comeile('\d{13}') # 先定义在拿来用

match*

s = 'abcd abcddd abc'
res = re.match('abcd*',s)
print(res.group)    # 从开头找一个找到就不继续找了,找不到就报错
 # abcd
s = 'abcd abcddd abc'
res = re.match('abcd*',s)
print(res.group)    # 从从字符串中找一个,找不到就报错
 # abcd

split

s = 'abc1231d abcd12321dd a123bc'
res = re.split('\w+',s)
print(res.) # 以数字划开
 # ['abc', 'd abcd', 'dd a', 'bc']

sub(replace)

s = 'abc1231d abcd12321dd a123bc'
res = re.sub('\w+','',s)
print(res.) # 将数字替换为''
 # [abcd abcddd abc]

supplement

Modifiers -.> Re.S will match a newline

s = '''abc
abcabc*abc
'''

# .不匹配换行
print(re.findall('abc.abc', s))  # ['abc*abc']
print(re.findall('abc.abc', s, re.S))  # ['abc\nabc', 'abc*abc']

() Grouping -> as long as the brackets

s = 'abc abcd abcdd'
print(re.findall('a(.)c(d)', s))

 # [('b', 'd'), ('b', 'd')]

() Grouping known

s = 'abc abcd abcdd'
print(re.search('a(?P<name1>.)c(?P<name2>d)', s).groupdict())

Guess you like

Origin www.cnblogs.com/hj59988326/p/11610582.html