函数剩余部分

'''

函数三种定义方式

'''


#1.无参函数:

#不需要接受外部传入参数


#2.有参函数:

#   需要接受外部传入参数
# def login(user,pwd):
#     print(user,pwd)
#


#3.空函数:
#遇到一些比较难实现的功能,会导致暂时无法继续编写代码
#所以在一般生产开发中,会预留以便后面编写

# def func():
#     pass #pass代表什么都不做

'''
函数返回值
'''



'''
函数对象
    指的是函数名指向的内存地址
    函数的名字指的是函数所在内存地址
    加()表示调用
    
'''

# def func():
#     pass
#
# # print(func)
#
# def func2():
#     pass
#
#
# dict1 = {
#     '1':func,
#     '2':func2
# }
#
# choice = input('请输入功能编号:').strip()
# if choice in dict1:
#     dict1[choice]() #()表示调用
#

'''
函数嵌套:
    嵌套定义:
        在函数内定义函数。
    嵌套调用:
        
'''
#函数嵌套定义
def func1():
    print('func1')

    def func2():
        print('func2')

        def func3():
            print('func3')
        return func3

    return func2
#通过函数内部的函数值,调用函数
func2 = func1()
func3 = func2()
func3()


#函数嵌套调用

# def func1():
#     print('func1..')
#
#     def func2():
#         print('func2...')
#
#         def func3():
#             print('func3...')
#
#         func3()
#
#     func2()
#
# func1()


'''
名称空间
Python解释器自带的:内置名称空间
自定义的py文件内,顶着最左边定义的:全局名称空间
函数内部定义的:局部名称空间
'''

name = 'tututu'
def func1():
    print(name)

    def func2():
        print('func...')

print(name,'全局打印')

func1()#局部打印

内置模块

'''
time 模块
json 模块
os
sys
'''


#time模块

# import time #导入time模块
# #获取时间戳
# print(time.time())
# #等待
# time.sleep(2)
# print(time.time())
#


#os模块

# #判断tututu。text文件是否存在
# import os
# #与操作系统中文件交互
# print(os.path.exists('tututu.text'))#判断文件
# print(os.path.exists(r'C:\Users\涂先生\PycharmProjects\untitled1\day02\tututu.text'))#判断绝对路径
#
# #获取当前文件的根目录
# print(os.path.dirname(__file__))
#

#sys模块

# import sys
# #获取Python在环境变量中的文件路径
# print(sys.path)
# #把项目的根目录添加到环境变量中
# sys.path.append(os.path.dirname(__file__))
#

#json模块
# import json
# user_info = {
#     'name':'tank',
#     'pwd':'123'
#     }

#dumps:序列化
#1、把字典转化成json数据
#2、再把json转化成字符串
# res = json.dumps(user_info)
# print(res)
# print(type(res))
#
# with open('user.json','wt',encoding='utf8')as f:
#     f.write(res)

#loads:反序列化
#json.loads()
#1、把json文件的数据读入内存
with open('user.json','r',encoding='utf8')as f:
    #读取得到的是字符串
    res = f.read()
    # print(type(res))
    #load把json格式的字符串转化成dict类型

    user_dict = json.loads(res)
    print(user_dict)
    print(type(user_dict))

#dump
import json
user_info = {
    'name':'tank',
    'pwd':'123'
    }
with open('user_info.json','w',encoding='utf8')as f:
    json.dump(user_info,f)


#load
with open('user_info.json', 'r', encoding='utf8')as f:
    user_dict = json.load(f)
    print(user_dict)

模块与包

# import 模块名(文件夹)

import B

#from
#导入B模块中的a文件
#会自动执行a文件中的代码

from B import a

#__name__:B.a
#a

爬虫

'''
http协议:
    请求url:
        https://www.baidu.com/

    请求方式:
        get

    请求头:
        Cookie:可能需要关注
        Host: www.baidu.com(可能会带上)
        User-Agent:证明你是浏览器
         Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36

'''


#requests模块
#pip3 install requests
#pip3 install -i 清华源地址 模块名
#pip3 install https://pypi.tuna.tsinghua.edu.cn/simple(清华源)

# import requests
#
# response = requests.get(url='https://www.baidu.com/')
# response.encoding = 'utf-8'
# #
# print(response)#<response[200]>
#
# #返回响应状态码
# print(response.status_code)
# #返回响应文本
# print(response.text)
# print(type(response.text))
#
# with open('baidu.html','w',encoding='utf-8')as f:
#     f.write(response.text)
#

#爬取梨视频
import requests
res = requests.get('https://video.pearvideo.com/mp4/adshort/20190613/cont-1565846-14013215_adpkg-ad_hd.mp4')

print(res.content)

with open('视频.mp4','wb')as f:
    f.write(res.content)