模块 time,datetime,random,hashlib,requests

package

What is the package

Is a form of a module package, is contained .pyfile folders

Why package

At first only a few function modules, the next extension module, the module name and usage should be best not to change, but it is convenient for users, and for developers, module to manage a lot of trouble, so I used the bag to extend the functionality of the module.

1. Introduction package

  1. Essentially module actually contains a __init__.pyfile folder
  2. Guide package is to import the __init__.pyfile
  3. Package must be treated as file import module, the module search path to the file path to the executable file, whichever

2. Absolute and relative import import

It can only be used in the package

- absolute imports

from the package name. import module name method name

- relative imports

  • The representative of the current file import folder is located
  • .. on the first level represents the current file is located in the import folder
  • ... on behalf of the current level on a file where the import folder

time module

Timestamp

import time

print(time.time())   # 从1970年1月1日00:00:00开始计算到现在的秒数

Formatting time

import time 
print(time.strftime('%Y-%m-%d %H:%M:%S'))  # 2019-09-28 17:15:47

print(time.strftime('%Y-%m-%d %X'))   # 2019-09-28 17:16:50

Structured time

import time
print(time.localtime())   # time.struct_time(tm_year=2019, tm_mon=9, tm_mday=28, tm_hour=17, tm_min=18, tm_sec=11, tm_wday=5, tm_yday=271, tm_isdst=0)
# 结构化基础时间
import time
print(time.localtime(0))   # time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

sleep

import time 

start = time.time()
time.sleep(2)
end = time.time()
print(f'暂停了{end - start}秒')  # 暂停了2.000108003616333秒

datetime module

import datetime

# 输出当前时间
print(datetime.datetime.now())
# 2019-09-28 17:25:24.551237


# 加时间
now = datetime.datetime.now()
print(now + datetime.timedelta(days=3))
# 2019-10-01 17:28:24.710093


print(now.replace(year=1940))
# 1940-09-28 17:29:45.066855

random module

import random

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


# 1-100随机整数
print(random.randint(1,100))

# 1-3之间随机整数
print(random.randrange(1, 3))

# 打乱lt的顺序
lt = [1,2,4,60]
random.shuffle(lt)   # [4, 1, 60, 2]
print(lt)

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

# random.seed
import random

random.seed(4)   # 给一个随机数种子
print(random.random())   # 只第一次随机生成,之后生成的数字就一样了
print(random.random())

# 如果不自定义种子,则种子按照当前的时间来

hashlib module

What is hash

hash is an algorithm (Python3. hashlib module version instead of using md5 and sha module module, the main provider SHA1, SHA224, SHA256, SHA384, SHA512, MD5 algorithm), the algorithm accepts incoming content, through the operation get a bunch of hash value.

import hashlib

m = hashlib.md5()
m.update(b'sayhello')   # 981fe96ed23ad8b9554cfeea38cd334a
print(m.hexdigest())   # 对于不同的字符而言,永不重复

Hit to break the hash algorithm library

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

hash_pwd = '0562b36c3c5a3925dbe3c4d32a4f2ba2'

for pwd in pwd_list:
    m = hashlib.md5()
    m.update(pwd.encode('utf-8'))
    res = m.hexdigest()
    if res in hash_pwd:
        print(f'获取密码成功:{pwd}')  # 获取密码成功:hash123456

hmac module

Key salt

import hmac

m = hmac.new(b'haha')
m.update(b'hash123456')
print(m.hexdigest())    # 24bb8daab11e526fc9b8178e51bc2ae7


m = m = hmac.new(b'sadness')
m.update(b'hash123456')
print(m.hexdigest())   # df405ffd019d6d3cd9a190fcab33aca5

requests module

Data may be used to crawl, the browser sends a request to the analog url, get data

import requests

response = requests.get('https://www.baidu.com')
print(response.text)

re module

String to find String meet certain characteristics

The use substantially re module:

import re  # 第一步,要引入re模块
a = re.findall("匹配规则", "这个字符串是否有匹配规则的字符")  # 第二步,调用模块函数
print(a)  # 以列表形式返回匹配到的字符串

^ Character

To ... beginning

s = 'abcdabc'

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

$ Characters

Ending ......

s = 'abcdabc'

res = re.findall('bc$',s)
print(res)             # ['bc']

: Any character

s = 'abc是dabc'

res = re.findall('abc.',s)
print(res)   # ['abc是']

\ D: Digital

s = 'asdhg213214h4c'

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

\ D: Non-digital

s = 'asdhg2132 -14h4c'

res = re.findall('\D',s)
print(res)   # ['a', 's', 'd', 'h', 'g', ' ', '-', 'h', 'c']

\ W: non-empty, numbers, letters, underscores

s = 'asdhg213214h4c'

res = re.findall('\w',s)
print(res)   # ['a', 's', 'd', 'h', 'g', '2', '1', '3', '2', '1', '4', 'h', '4', 'c']

\ W: empty, except for numbers, letters, underscores outside

s = 'as;g:21?32    -14h4c\n'

res = re.findall('\W',s)
print(res)  # [';', ':', '?', ' ', ' ', ' ', ' ', '-', '\n']

\ S: empty

s = 'asdhg2132 14h4c'

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

\ S: is not empty

s = 'asdhg2132    -14h4c\n'

res = re.findall('\S',s)
print(res)  # ['a', 's', 'd', 'h', 'g', '2', '1', '3', '2', '-', '1', '4', 'h', '4', 'c']

+: In front of a character at least one

s = 'abcdddd abcd abc ab'
print(re.findall('abc+', s))

?: One in front of the characters 0-1

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

*: 1 character in front of at least 0

s = 'abcdddd abcd abc ab a'
print(re.findall('abcd*', s))   # ['abcdddd', 'abcd', 'abc']
s = 'abc bbc cbc dbc'
print(re.findall('[abc]bc', s))  # ['abc', 'bbc', 'cbc']

[^]: In brackets can not

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

|: Or

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

{2}: preceding character 2

s = 'abcccab abcc'
print(re.findall('abc{2}', s))   # ['abcc', 'abcc'] 
print(re.findall('abc{0,2}', s))  # ['abcc', 'ab', 'abcc']

Greedy

(Any character) * (0- infinite number)

s = 'abcdefgaaaaaaaaaaag'
print(re.findall('a.*g',s))
# ['abcdefgaaaaaaaaaaag']

Non-greedy mode

(Any character) * (0- infinite number)?

s = 'abcdefgbbbbbbbg'
print(re.findall('a.*?g',s))   # ['abcdefg']

Learn: Special construction

# a(?=\d) :a后面是数字,但是不要数字,不消耗字符串内容
s = 'a123 aaaa a234 abc'
print(re.findall('a(?=\d)', s))  #['a', 'a']

print(re.findall('a(?=\w)', s))  #['a', 'a', 'a', 'a', 'a', 'a']

compile

# 早期,re.findall不能传模式,只能用compile
s = '#@#@#@[email protected]$$$$////[email protected]$$#$#$[]][email protected]@$2423423lksdlfj#'
email_pattern = re.compile('\w+@\w+.com')
phone_patter = re.compile('\d{13}')
print(re.findall(email_pattern, s))

match: a look from the beginning, is not looking to find, can not find on the error

s = 'abcd abcddd abc'
res = re.match('abcd*', s)
print(res.group())  # abcd

search: looking from a string, not looking

s = 'abcd abcddd abc'
res = re.search('abcd*', s)
print(res.group())  # abcd

split Segmentation

s = 'adad213114242wjdnadjia1241423daj'
print(re.split('\d+', s))  # ['adad', 'wjdnadjia', 'daj']

Replace sub

s = 'adad213114242wjdnadjia1241423daj'
print(re.sub('\d+', '  ', s))  # adad  wjdnadjia  daj

subn replaced more than sub replaced many times

s = 'adad213114242wjdnadjia1241423daj'
print(re.subn('\d+', '  ', s))   # ('adad  wjdnadjia  daj', 2)

Supplementary: re.S

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 abcddd'
print(re.findall('a(.)c(d)', s))  # [('b', 'd'), ('b', 'd')]

Famous grouping

s = 'abc abcd abcddd'
print(re.search('a(?P<name1>.)c(?P<name2>d)', s).groupdict()) # {'name1': 'b', 'name2': 'd'}

Ultra Advanced Usage

s = 'abc123abc123'  # c123a
print(re.sub('c(\d+)a', ' ', s))
print(re.sub('c(?P<name1>\d+)a', ' \g<name1> ', s))  # \g<name1>这个东西不能替换掉

# ab bc123
# ab 123 bc123

Guess you like

Origin www.cnblogs.com/setcreed/p/11628032.html