自我总结16

当模块内部函数过多,为了方便管理模块,把一个模块划分成多个模块,但是又不能改变导入方式,把多个# 模块放入一个包(文件夹)内。未来导包就是导init

# 1. 包 == 模块, 包拿来导入用的
# 2.包是含有__init__.py的文件夹; 导包就是导入__init__
# 3.包一定是被当作模块文件导入,模块文件 m1.py/m2.py 的搜索路径以执行文件 包的介绍.py 路径为准

# 相对导入绝对导入: 只能在包中内部使用

time模块

提供了三种不同类型的时间(时间戳),三种不同类型的时间可以相互转换

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)

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模块

import hashlib


m = hashlib.md5()
m.update(b'say') 
print(m.hexdigest())# 对于不同的字符而言,用不重复
#a53ff64efd169c1b4d085d6e7075c8d7
# 手机号/生日/性别/qq账号/以前的密码/   --》 挖矿(算法)

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}')

hamc模块

密钥(加盐)

import hmac

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

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

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

typing模块

与函数联用,控制函数参数的数据类型,提供了基础数据类型之外的数据类型

lt = [1, 2, 3, 4]
print(type(lt) is list) # True 
from typing import Iterable, Iterator, Generator

print(lt == Iterable)  # False

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

func(10, '123123')

requests模块

爬虫--》爬数据,模拟浏览器对url发送请求,拿到数据

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

response = requests.get('url')
data = response.text
print(data)

re模块

# .*?表示所有类型的都要
# 补充:.*?不同的一类信息都忽略,(.*?)不同的一类信息都保留下来

import requests

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

# zip生成字典方法
import re

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

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)
    
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 |方法
import re
import requests

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

res = re.findall('<div class="content">(.*?)</div>|</span><a href="/subject/.*?">(.*?)</a>', data)

with open('duanzi_new.txt', 'w', encoding='utf8') as fw:
    for i in res:  # type:str
        print(i)
        if i[1]:
            fw.write(i[1] + ':' + '\n\n')
        if i[0]:
            if i[0].startswith('<ul>'):
                continue
            fw.write(i[0] + '\n')

掌握

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



s = '去字符串找符合某种特点的字符串'

res = re.findall('', s)
print(res)



# 贪婪模式
# .(任意字符)*(0-无穷个)

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

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

# bug
# .(任意字符)*(0-无穷个)?(让他进入非贪婪模式)
s = 'abcdefg'
print(re.findall('.*?', 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(res.group())


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

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

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

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

了解

# 杂七杂八的元字符
# 特殊构造元字符
# 特殊修饰符


# 了解:特殊构造

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

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





# 元字符

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

## subn --> 替换了多少次
s = 'ab23423abcddd234234abcasdfjlasjdk234l23lk4j2kl34kl25k3j2kl3j5lkj'
print(re.subn('\d+', ' ', s))
posted @ 2019-10-03 13:30  jzm1201  阅读(103)  评论(0编辑  收藏  举报