zoukankan      html  css  js  c++  java
  • day 17

    day 17 time datetime random hashlib hmac typing requests re模块

    01.time模块;提供了三种不同类型的时间(时间戳)。三种不同类型的时间可以互相转换

    import time
    time.time() # 获取当前时间节点
    time.sleep(1) # 暂停一秒钟
    
    1. 时间戳

      print(time.time()) # 打印当前距1970年1月1日08:00:00 按秒的时间啊偏移量
      
    2. 格式话时间

      format_time = time.strftime('%Y-%m-%d %X') # 中间的 - 只是连接符
      print(format_time, type(format_time))
      2019-09-28 20:33:01 <class 'str'>  #打印结果
      
    3. 结构化时间

      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)
      print(time.localtime()) # 打印当前时间
      time.struct_time(tm_year=2019, tm_mon=9, tm_mday=28, tm_hour=20, tm_min=11, tm_sec=10, tm_wday=5, tm_yday=271, tm_isdst=0)
      # 分别为(年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时)
      
    4. 结构化时间啊转换为格式化时间

      struct_time = time.localtime(3600*24*365) # 从1970年1月1日00:00:00开始增加
      print(time.strftime('%Y-%m-%d %X',struct_time))
      1971-01-01 08:00:00
      
    5. 格式化时间转化为结构化时间

      format_time = time.strftime('%Y-%m-%d %X')
      print(time.strptime(format_time))
      time.struct_time(tm_year=2019, tm_mon=9, tm_mday=28, tm_hour=20, tm_min=49, tm_sec=17, tm_wday=5, tm_yday=271, tm_isdst=-1)
      
    6. 结构化时间转化为时间戳

      sturct_time = time.localtime(3600*24*365)
      print(time.mktime(sturct_time))
      
    7. 时间戳转化为结构化时间

      time_stamp = time.time()
      print(time.localtime(time_stamp))
      

    02.datetime模块;时间的加减

    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))
    # 打印自己设置的时间
    print(now.replace(year=1949, month=10, day=1, hour=10, minute=1, second=0, microsecond=0))
    
    

    03.hashlib模块;对字符加密

    import hashlib
    # 叠加性
    m = hashlib.md5()
    m.update(b'say')
    m.update(b'hello')  # 981fe96ed23ad8b9554cfeea38cd334a
    m.update(b'hash123456')  # 6c78fe5d027cee9724ed0793cd4096e8
    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}')
    
    

    05.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',
    ]
    
    

    06.typing模块;与函数连用,控制函数参数的数据类型,提供了基础数据类型之外的数据类型

    lt = [1, 2, 3, 4]
    print(type(lt) is list)
    
    from typing import Iterable, Iterator, Generator
    
    def func(x: int, lt: Iterable) -> list:  # 规定型参接受的数据类型,返回值的类型
        return [1, 2, 3]
    
    
    func(10, '123123')
    
    

    typing常用数据类型

    • int、long、float: 整型、长整形、浮点型
    • bool、str: 布尔型、字符串类型
    • List、 Tuple、 Dict、 Set:列表、元组、字典、集合
    • Iterable、Iterator:可迭代类型、迭代器类型
    • Generator:生成器类型

    07.requests模块;模拟浏览器发送请求

    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] + ':' + '
    
    ')
            if i[0]:
                if i[0].startswith('<ul>'):
                    continue
                fw.write(i[0] + '
    ')
    
    

    08.re模块 通过正则匹配获取需要的内容

     
    import re
    s='com.con.cn.cm.china'
    print(re.findall('((w+.){2})',s)) 
    # .*?
    #贪婪和非贪婪 .*?;匹配所有内容, (.*?);获取内容
    #findall 获取想要的内容
    #re.S
    #match 从开头找一个,找得到就不找了 ;找不到报错 --》必须是以该内容开头的字符串才能找到
    s = 'ab abcddd abc'
    res = re.match('abcd*', s)
    print(res.group())
    #sarch 从字符串中找一个
    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())
    
    
    
  • 相关阅读:
    Hyper-V无法启动虚拟机因为虚拟机监控程序未运行
    SpringBoot项目中自动加载datasourceConfig配置导致启动失败
    redis 数据类型与命令
    Redis入门与安装,与配置
    MySQL 主从配置
    MySql 中的事务
    什么是Docker?
    window10下安装Docker
    Docker 常见命令
    原生SQL语句
  • 原文地址:https://www.cnblogs.com/luocongyu/p/11630417.html
Copyright © 2011-2022 走看看