今天是学习python的第三天,今天主要讲了昨天剩余的有关函数的知识,接着讲了模块,最后讲了一点关于爬虫的知识。
课堂笔记:
1.函数部分
.定义函数的三种方式
*无参函数
# 不需要接收外部传入的参数
*有参函数
# 需要接收外部传入的参数
注:传参多一或少一不可
*空函数
# 遇到一些比较难实现的功能,会导致暂时无法继续编写代码。
# 所以一般在生产开发中,都会将所有功能实现定义成空函数。
''
# # 无参函数
# # 不需要接收外部传入的参数
# def foo():
# print('from foo..')
# foo()
#
#
#
# # 有参函数
# # 需要接收外部传入的参数
# def login(user, pwd):
#
# print(user, pwd)
# #
# # # 传参多一或少一不可
# login('tank', '123')
# # login('tank', '123', 111) # 多,报错
# # login('tank') # 少,报错
#
# # x = 10
# # y = 20
# #
# # if x > y:
# #
# # print(x)
# #
# # else:
# # print(y)
#
# # 比较两数大小
# def max2(x, y):
#
# if x > y:
#
# print(x)
#
# else:
# print(y)
#
# max2(10, 30)
#
2.函数的返回值
在调用函数时,需要接收函数体内部产生的结果,则return返回值。
# def max2(x, y): # # if x > y: # # return x # # else: # # return y # # res = max2(10, 5) # # print(res)
3.函数对象
指的是函数名指向的内存地址
# def func():
# pass
#
# # print(func) # <function func at 0x101dd2e18>
# #
# # func()
#
# def func2():
# pass
#
# # 把函数对象,传入字典中
# dict1 = {
# '1': func,
# '2': func2
# }
#
# choice = input('请输入功能编号:').strip()
#
# # if choice == '1':
# # func()
# # elif choice == '2':
# # func2()
#
# #
# # 若用户选择函数对象对应的key值,则调用该函数
# if choice in dict1:
# dict1[choice]() # dict1['1']
4.函数嵌套:
嵌套定义:
在函数内,定义函数。
嵌套调用:
# 函数嵌套定义
#
# 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()
5.名称空间
python解释器自带的: 内置名称空间
自定义的py文件内,顶着最左边定义的: 全局名称空间
函数内部定义的: 局部名称空间
name = 'tank'
def func1():
# name = 'tank'
print()
def func2():
print('func2...')
# print(name, '全局打印')
func1()
6.模块与包
# import 模块名
import B
# from
# 导入B模块中的a文件
# 会自动执行a文件中的代码
# import 模块名 import B # from # 导入B模块中的a文件 # 会自动执行a文件中的代码 from B import a # __name__: B.a # a
7.内置模板
01.time
02.os
03.sys
04.json
''''''
'''
常用模块(内置模块)
'''
# time
# import time # 导入time模块
# # 获取时间戳
# print(time.time())
#
# # 等待2秒
# time.sleep(2)
#
# print(time.time())
# os
import os
# 与操作系统中的文件进行交互
# 判断tank.txt文件是否存在
# print(os.path.exists('tank.txt')) # True
# print(os.path.exists('tank1.txt')) # False
# print(os.path.exists(r'D:python_filesday03 ank.txt')) # True
#
# # 获取当前文件的根目录
# print(os.path.dirname(__file__)) # D:/python_files/day03
# sys
import sys
# 获取python在环境变量中的文件路径
# print(sys.path)
# # 把项目的根目录添加到环境变量中
# sys.path.append(os.path.dirname(__file__))
# print(sys.path)
# 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='utf-8') as f:
# f.write(res)
# loads: 反序列化
# json.loads()
# 1、把json文件的数据读到内存中
# with open('user.json', 'r', encoding='utf-8') as f:
# # 读取得到的是字符串
# res = f.read()
# # print(type(res))
# # loads把json格式的字符串转换成dict类型
# user_dict = json.loads(res)
# print(user_dict) # {'name': 'tank', 'pwd': '123'}
# print(type(user_dict)) # <class 'dict'>
# dump
# user_info = {
# 'name': 'tank',
# 'pwd': '123'
# }
# with open('user_info.json', 'w', encoding='utf-8') as f:
# # str1 = json.dumps(user_info)
# # f.write(str1)
# # dump: 自动触发f.write方法
# json.dump(user_info, f)
# load
with open('user_info.json', 'r', encoding='utf-8') as f:
# res = f.read()
# user_dict = json.loads(res)
# print(user_dict)
# load:自动触发f.read()
user_dict = json.load(f)
print(user_dict)
8.爬虫基础知识
http协议:
请求url:
https://www.baidu.com/
请求方式:
GET
请求头:
Cookie: 可能需要关注。
User-Agent: 用来证明你是浏览器
注意: 去浏览器的request headers中查找
01 爬取网站
02爬取梨视频
# 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://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)