Python Day03
1.函数剩余部分
#无参函数:不需要接受外部传入的参数
#有参函数:需要接受外部传入的参数,传参数多一不可,少一不可
#空函数:遇到一些比较难以实现的功能,会导致暂时无法继续编写代码,所以,一般在生产开发中,会将所有功能都先定义为空函数
#函数的返回值:在调用函数时,需要接受函数内部的产生结果,则用return接收
#函数对象:函数名指向的内存地址
#函数嵌套:
#(1)嵌套定义:在函数内定义函数
#(2)嵌套调用:通过函数内部的函数值,调用函数
#名称空间:python解释器自带的:内置名称空间
#自定义的py文件中,顶着最左边定义的,全局名称空间
#函数内部定义的:局部名称空间
#无参函数 def foo(): print('from foo') foo() #有参函数 def login(user,pwd): print(user,pwd) login('lee',111) def max(x,y): if(x>y): print(x) else: print(y) max(10,20) #空函数 def func(): pass #什么都不做 func() #函数的返回值 def max(x,y): if(x>y): return x else: return y res=max(10,20) print(res) #函数对象 def func(): pass #什么都不做 print(func()) func() def func2(): pass #把函数对象放入字典中 #若用户选择对应的key值,就调用函数 dict1={'1':func,'2':func2} choice=input('输入功能标号:').strip() if choice in dict1: dict1[choice] #嵌套定义: def func(): print('func') def func2(): print('func2') def func3(): print('func3') #嵌套调用: def func(): print('func') def func2(): print('func2') def func3(): print('func3') return func3 func3() return func2 func2() func() func2=func() func3=func2() func3()
#名称空间 name='lee' def func(): name='lee' print(name) def func2(): print(name) func()
2.内置模块
#内置模块
'''
1.time
2.json
3.os
4.sys
'''
#1.time模块 import time #获取时间戳 print(time.time()) #等待 time.sleep(1) #等待10秒 print(time.time()) #3.0s模块 import os #判断输入的文件是否存在 print(os.path.exists('01.py')) #判断其绝对路径 print(os.path.exists('D:\pycharm\B\a.py')) #获取当前文件根目录 #print(os.path.dirname()) #4.sys模块 import sys #获取Python在环境变量中的文件路径 print(sys.path)
#2.json模块 import json user_info={'name':'lee','age':18,'sex':'male'} #dumps:序列化 #1.把字典转行成json数据 #2.把json数据转成字符串 res=json.dumps(user_info) print(res) print(type(res)) with open('user.txt','wt',encoding='utf-8')as f: f.write(res) #json.loads():反序列化 #1.把json文件读取到内存 #2.loads把json格式的字符串转换成dict类型 with open('user.txt','r',encoding='utf-8')as f: res=f.read() user_dict=json.loads(res) print(user_dict) print(type(user_dict)) #json.dump():自带write功能,直接传入文件句柄即可,自动触发write方法 with open('user_info.json','wt',encoding='utf-8')as f: json.dump(user_info,f) #json.load(): with open('user_info.json','r',encoding='utf-8')as f: user_dict1=json.load(f) print(user_dict1)
3.模块与包
#import 模块名 import B #from #导入B模块中的a文件 #自动执行a中的代码 #from B import a from B import a #_name_=B.a #a
4.爬虫基础知识
# ''' http协议: 请求URL: http://www.baidu.com/ 请求方式: GET 请求头: Cookie:可能需要关注 User_agent:证明你是浏览器,而非机器人 注意:去浏览器的requests headers 中查找 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36 OPR/60.0.3255.151 Host:www.baidu.com ''' #requests模块使用 #pip3 install requests #pip3 install -i 清华园地址 模块名 #pip3 install -i http://pypi.tuna.tsinghua.edu.cn/simple resquests ''' import requests response=requests.get(url='http://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(url='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)