day3
1函数剩余部分
2内置模块
3模块与包
# def login(): # nuber=0 # with open('user.txt','r',encoding='utf-8')as f: # res=f.read() # print(res) # username,password=res.split(':') # flag=True # while flag: # user=input('').strip() # if username==user: # if number==3: # break # while number<3: # # pwd=input('qinshurumima:').strip() # # if password==pwd: # # print('') # login() # # #空函数 # # #遇到一些比较难实现的功能 # # def func(): # # pass #代表什么都不做 # # #函数的返回值 # # 在调用函数时 # # def max2(x,y): # # if(x>y): # # return x # # else: # # return y # # res=max2(10,5) # # print(res) # """函数参数 # # 名称空间 # Python解释器自带的内置名称空间 # 自定义的py文件内,顶着最左边写的:全局名称空间 # 函数内部定义的:局部名称空间 # # # def func1() # print() # # def func2(): # print('func2...') # # func1() # """ # #import 模块名 # # # # # """ # time # json # os # sys # """ # #time # import time#导入time模块 # #获取时间戳 # print(time.time()) # # #等待两秒 # time.sleep(2) # print(time.time()) # #os # import os # #判断文件是否存在 # print(os.path.exists('user.txt'))#True # print(os.path.exists('user1.txt'))#False # #获取当前文件的根目录 #sys import sys #获取python在环境变量中的文件路径 print(sys.path) ''' http协议: 请求url: https://www.baidu.com/ 请求方式: GET 请求头: Cookie: 可能需要关注。 User-Agent: 用来证明你是浏览器 注意: 去浏览器的request headers中查找 Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36 Host: www.baidu.com ''' # requests模块使用 # pip3 install requests # pip3 install -i 清华源地址 模块名 # pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple requests # import requests # # response = requests.get(url='https://www.baidu.com/') # response.encoding = 'utf-8' # print(response) # <Response [200]> # # 返回响应状态码 # print(response.status_code) # 200 # # 返回响应文本 # # print(response.text) # print(type(response.text)) # <class 'str'> # # with open('baidu.html', 'w', encoding='utf-8') as f: # f.write(response.text) 作业: 爬取哔哩哔哩 import requests res = requests.get('https://www.bilibili.com/video/av55400467') print(res.content) with open('v.mp4', 'wb') as f: f.write(res.content)