day17 summary

package

== bag module, used to package the introduction

为什么要有包

# 对用户而言不友好的

# import aaa
#
# aaa.f1()
#
#
# aaa.f3()


# m1.f1()
#
# from aaa import m2
#
# m2.f3()
#
#
#
# import aaa
#
# aaa.f1()
#
#
# aaa.f3()


# 2.包是含有__init__.py的文件夹; 导包就是导入__init__


# 3.包一定是被当作模块文件导入,模块文件 m1.py/m2.py 的搜索路径以执行文件 包的介绍.py 路径为准


# import aaa
#
# print(aaa.f1)
# print(aaa.f2)
#
# print(aaa.f3)

# from aaa.m1 import f1  # 不符合包的原则
# f1()


# 了解


# from aaa.bbb.m3 import f5
#
# f5()

import aaaa

print(aaaa.f5)

Absolute introduced relative Import: only used internally in the package

Packages are: when too many functions within the module, in order to facilitate the management module, the module is divided into a plurality of modules, but do not change the way of introduction, the plurality of
modules into a package (folder) inside. Init package is to guide future guide

time module

time module: provides three different types of time (time stamp), three different types of interchangeable times

import time

print(time.time())  # 时间戳形式

# 格式化时间
print(time.strftime('%Y-%m-%d %X'))

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


# 结构化时间 --》 格式化时间
struct_time = time.localtime(3600*24*365)
print(time.strftime('%Y-%m-%d %X',struct_time))


# 格式化时间 --》 结构化时间
format_time = time.strftime('%Y-%m-%d %X')
print(time.strptime(format_time,'%Y-%m-%d %X'))


# 结构化时间 --》 时间戳
struct_time = time.localtime(3600*24*365)
print(time.mktime(struct_time))

# 时间戳 --》 结构化时间
time_stamp = time.time()
print(time.localtime(time_stamp))



最重要的(*******)
time.time()
time.sleep(1)

datetime module

datetime module: add and subtract time

import datetime

now = datetime.datetime.now()
print(now)

# 默认3天
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))

print(now.replace(year=1949, month=10, day=1, hour=10, minute=1, second=0, microsecond=0))xxxxxxxxxx # datetime模块:时间的加减import datetimenow = datetime.datetime.now()print(now)# 默认3天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))print(now.replace(year=1949, month=10, day=1# datetime模块:时间的加减import datetimenow = datetime.datetime.now()print(now)# 默认3天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))print(now.replace(year=1949, month=10, day=1, hour=10, minute=1, second=0, microsecond=0))

random module

random module: Random number

import random


# 掌握

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

# [1-3]
print(random.randint(1,3))

# 打乱
lt=[1,2,3]
random.shuffle(lt)
print(lt)

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

# 只随机一次  --> 梅森旋转算法
import time
# random.seed(time.time())
# random.seed(111111111111)
print(random.random())

# 了解
print(random.sample([1,'a','c',2,3,4],2))

hashlib module module hmac

hashlib Module: encryption character
hmac Module: encryption of the character, and the key plus

# import hashlib
#
# # 叠加性
# m = hashlib.md5()
# # m.update(b'say')
# # m.update(b'hello')  # 981fe96ed23ad8b9554cfeea38cd334a
# m.update(b'hash123456')
# print(m.hexdigest())  # 对于不同的字符而言,用不重复
#
# # 981fe96ed23ad8b9554cfeea38cd334a
#
# # 手机号/生日/性别/qq账号/以前的密码/   --》 挖矿(算法)
#
# # 1 2 3 5 71113 111111111111111 - 1111111111111111111111 111111111111111111111111111111111111111111111111111
#
# hash_pwd = '0562b36c3c5a3925dbe3c4d32a4f2ba2'
#
# pwd_list = [
#     'hash3714',
#     'hash1313',
#     'hash94139413',
#     'hash123456',
#     '123456hash',
#     'h123ash',
# ]
#
# for pwd in pwd_list:
#     m = hashlib.md5()
#     m.update(pwd.encode('utf8'))
#     res = m.hexdigest()
#     if res == hash_pwd:
#         print(f'获取密码成功:{pwd}')
#
# # 《动物庄园》 《1984》 《美丽新世界》: 如果不想被别人忽悠的话,看这三本书


# hmac 密钥(加盐)

import hmac

m = hmac.new(b'maerzi')
m.update(b'hash123456')  # f82317e44545b0ab087109454814b5c4
print(m.hexdigest())

m = hmac.new(b'sdfjhjk2394879ul%$$Y#($&')
m.update(b'hash123456')  # 2a70fd0f13cb49357f40d326a4e071a2
print(m.hexdigest())

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

# 0.03%

# 正太分布

typing module

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(type(lt) is list)

from typing import Iterable, Iterator, Generator


# print(lt == Iterable) # False

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


func(10, '123123')

requestes module

Module requests: reptiles - "crawl data, the browser sends a request to the analog url, get data

 url ——> 一个特定的网址 -》 用不重复
import requests

response = requests.get('https://ishuo.cn')
data = response.text
# print(data)


# 正则re:从大的字符串中挑选出 具有某种形状特点的字符串

import re

# .*?表示所有类型的都要
content_list = []
content_res = re.findall('<div class="content">(.*?)</div>', data)
for i in content_res:  # type:str
    if i.startswith('<ul>'):
        continue
    content_list.append(i)

# print(content_list)


title_list = re.findall('</span><a href="/subject/.*?">(.*?)</a>', data)

# print(title_list)

# title_content_dict = {}
# for i in range(len(content_list)):
#     title_content_dict[title_list[i]] = content_list[i]

title_content_dict = {k: v for k, v in zip(title_list, content_list)}

with open('duanzi.txt', 'w', encoding='utf8') as fw:
    for k, v in title_content_dict.items():
        fw.write(f'{k}:\n{v}\n\n')

re module

re module: to find a string that matches a string of features

import re

# s = '去字符串找符合某种特点的字符串'
#
# res = re.findall('', s)
# print(res)


# 元字符

s = 'abcdabc'
#    abc
#        abc
#     bc  bc

# ^:以...开头
res = re.findall('^ab', s)
print(1,res)
res = re.findall('^bc', s)
print(2,res)
# $: 以..结尾
s = 'abcdabc'
res = re.findall('bc$', s)
print(3,res)

# .: 任意字符
s = 'abc红凯abc'
res = re.findall('abc.', s)
print(4,res)

# \d: 数字
s = 'skld2342ljk'
res = re.findall('\d', s)
print(5,res)

# \w: 非空,数字字母下划线
s = 'skld_23 42ljk'
res = re.findall('\w', s)
print(6,res)

# \s:空,空格/\t/\n
s = 'skld_23 42ljk'
res = re.findall('\s', s)
print(7,res)

# \D: 非数字
s = 'skld2342ljk'
res = re.findall('\D', s)
print(8,res)

# \W: 空
s = 'skld_23 42ljk'
res = re.findall('\W', s)
print(9,res)

# \S:非空
s = 'skld_23 42ljk'
res = re.findall('\S', s)
print(10,res)

# +: 前面的一个字符至少1个
s = 'abcdddddabcdabc'
print(11,re.findall('abcd+', s))

# ?:前面的一个字符0-1个
s = 'abcdddhdd abcd abc'
print(12,re.findall('abcd?', s))

# *:前面的一个字符至少0个
s = 'ddabcddddddh abcd abc'
print(13,re.findall('abcd*', s))

# []: 中括号内的都可以
s = 'abc bbc cbc dbc'
print(14,re.findall('[abc]bc', s))

# [^]: 中括号的都不可以
s = 'abc bbc cbc dbc'
print(15,re.findall('[^abc]bc', s))

# |:或
s = 'abc bbc dbc'
print(16,re.findall('abc|bbc', s))

# {2}:前面的字符2个

s = 'ababc abccc'
print(17,re.findall('abc{2}', s))

# {1,2}:前面的字符2个

s = 'abccabc abccc'
print(18,re.findall('abc{1,2}', s))

# 贪婪模式

# .(任意字符)*(0-无穷个)

s = 'abcdefgbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbg'
print(19,re.findall('a.*g', s))

# 非贪婪模式(*******)

# .(任意字符)*(0-无穷个)?(让他进入非贪婪模式)
s = 'abcdefgbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbg'
print(20,re.findall('a.*?g', s))

# bug
# .(任意字符)*(0-无穷个)?(让他进入非贪婪模式)
s = 'abcdefg'
print(21,re.findall('.*?', s))

# 了解:特殊构造


# a(?=\d) :a后面是数字,但是不要数字,不消耗字符串内容
s = 'a123 aaaa a234 abc'
#    a1    aa
#           aa
#            aa a2    ab
print(22,re.findall('a(?=\d)', s))
print(23,re.findall('a(?=\w)', s))

# 匹配邮箱:
s = '#@#@#@[email protected]$$$$////[email protected]$$#$#$[]][email protected]@$2423423lksdlfj#'
# \w(字母/数字/下划线)+(0-无穷个)@ \w(字母/数字/下划线)+(0-无穷个).com
print(24,re.findall('\w+@\w+.com', s))

# 函数


## compile
s = 'abcd abcddd abc'
# res = re.compile('abcd*')
email_pattern = re.compile('\w+@\w+.com')
phone_patter = re.compile('\d{13}')
print(25,re.findall(email_pattern, s))

print(26,re.findall('abcd*', s))

# ## match:  从开头找一个,找得到就不找了 ;找不到报错 --》
# s = 'ab abcddd abc'
# res = re.match('abcd*', s)
# print(res.group())

## search: 从字符串找一个,就不找了
s = 'ab abcddd abc'
res = re.search('abcd*', s)
print(27,res.group())

## split
s = 'ab23423abcddd234234abcasdfjlasjdk234l23lk4j2kl34kl25k3j2kl3j5lkj'
print(28,re.split('\d+', s))

## sub == replace
s = 'ab23423abcddd234234abcasdfjlasjdk234l23lk4j2kl34kl25k3j2kl3j5lkj'
print(29,re.sub('\d+', ' ', s))

## subn --> 替换了多少次
s = 'ab23423abcddd234234abcasdfjlasjdk234l23lk4j2kl34kl25k3j2kl3j5lkj'
print(30,re.subn('dd.*?ab', ' ', s))

# 补充(非常有用)

## 修饰符 --> re.S会让.匹配换行符(*****)
s = '''abcfdg
abcabc*abc
'''

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

## 分组 --> 只要括号里的(*****)
s = 'abc abcd abcdd'
print(33,re.findall('a(.)c(d)', s))

## 有名分组(了解)
s = 'abc abcd abcdd'
print(34,re.search('a(?P<name>.)c(?P<name2>.*?)d', s).groupdict())

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


# 以下必须得记住

# .*?
# 贪婪和非贪婪
# findall
# re.S
# match和sarch的区别
# 分组
# 有名分组:给分组加名字

Guess you like

Origin www.cnblogs.com/zhm-cyt/p/11604589.html