第三次作业
1 '''''' 2 ''' 3 http协议: 4 请求url: 5 https://www.baidu.com/ 6 7 请求方式: 8 GET 9 10 请求头: 11 Cookie: 可能需要关注。 12 User-Agent: 用来证明你是浏览器 13 注意: 去浏览器的request headers中查找 14 Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36 15 Host: www.baidu.com 16 ''' 17 18 # requests模块使用 19 # pip3 install requests 20 # pip3 install -i 清华源地址 模块名 21 # pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple requests 22 23 # import requests 24 # 25 # response = requests.get(url='https://www.baidu.com/') 26 # response.encoding = 'utf-8' 27 # print(response) # <Response [200]> 28 # # 返回响应状态码 29 # print(response.status_code) # 200 30 # # 返回响应文本 31 # # print(response.text) 32 # print(type(response.text)) # <class 'str'> 33 # 34 # with open('baidu.html', 'w', encoding='utf-8') as f: 35 # f.write(response.text) 36 37 38 39 # 爬取梨视频 40 import requests 41 res = requests.get('https://video.pearvideo.com/mp4/adshort/20190613/cont-1565846-14013215_adpkg-ad_hd.mp4') 42 43 print(res.content) 44 45 with open('视频.mp4', 'wb') as f: 46 f.write(res.content)
内置模块
1 ''''''
2
3 '''
4 常用模块(内置模块)
5 '''
6
7 # time
8 # import time # 导入time模块
9 # # 获取时间戳
10 # print(time.time())
11 #
12 # # 等待2秒
13 # time.sleep(2)
14 #
15 # print(time.time())
16
17 # os
18 import os
19 # 与操作系统中的文件进行交互
20 # 判断tank.txt文件是否存在
21 # print(os.path.exists('tank.txt')) # True
22 # print(os.path.exists('tank1.txt')) # False
23 # print(os.path.exists(r'D:\python_files\day03\tank.txt')) # True
24 #
25 # # 获取当前文件的根目录
26 # print(os.path.dirname(__file__)) # D:/python_files/day03
27
28 # sys
29 import sys
30 # 获取python在环境变量中的文件路径
31 # print(sys.path)
32 # # 把项目的根目录添加到环境变量中
33 # sys.path.append(os.path.dirname(__file__))
34 # print(sys.path)
35
36
37 # json
38 import json
39 # user_info = {
40 # 'name': 'tank',
41 # 'pwd': '123'
42 # }
43
44 # dumps: 序列化
45 # 1、把字典转行成json数据
46 # 2、再把json数据转换成字符串
47 # res = json.dumps(user_info)
48 # print(res)
49 # print(type(res))
50 #
51 # with open('user.json', 'wt', encoding='utf-8') as f:
52 # f.write(res)
53
54 # loads: 反序列化
55 # json.loads()
56 # 1、把json文件的数据读到内存中
57 # with open('user.json', 'r', encoding='utf-8') as f:
58 # # 读取得到的是字符串
59 # res = f.read()
60 # # print(type(res))
61 # # loads把json格式的字符串转换成dict类型
62 # user_dict = json.loads(res)
63 # print(user_dict) # {'name': 'tank', 'pwd': '123'}
64 # print(type(user_dict)) # <class 'dict'>
65
66
67 # dump
68 # user_info = {
69 # 'name': 'tank',
70 # 'pwd': '123'
71 # }
72 # with open('user_info.json', 'w', encoding='utf-8') as f:
73 # # str1 = json.dumps(user_info)
74 # # f.write(str1)
75 # # dump: 自动触发f.write方法
76 # json.dump(user_info, f)
77
78
79 # load
80 with open('user_info.json', 'r', encoding='utf-8') as f:
81 # res = f.read()
82 # user_dict = json.loads(res)
83 # print(user_dict)
84
85 # load:自动触发f.read()
86 user_dict = json.load(f)
87 print(user_dict)
函数剩余部分
1 '''
2 '''
3
4 '''
5 名称空间
6 python解释器自带的: 内置名称空间
7 自定义的py文件内,顶着最左边定义的: 全局名称空间
8 函数内部定义的: 局部名称空间
9 '''
10
11
12 name = 'tank'
13
14 def func1():
15 # name = 'tank'
16 print()
17
18 def func2():
19
20 print('func2...')
21
22 # print(name, '全局打印')
23
24 func1()
25
26
27 ''''''
28 '''
29 定义函数的三种方式
30 '''
31 # # 无参函数
32 # # 不需要接收外部传入的参数
33 # def foo():
34 # print('from foo..')
35 # foo()
36 #
37 #
38 #
39 # # 有参函数
40 # # 需要接收外部传入的参数
41 # def login(user, pwd):
42 #
43 # print(user, pwd)
44 # #
45 # # # 传参多一或少一不可
46 # login('tank', '123')
47 # # login('tank', '123', 111) # 多,报错
48 # # login('tank') # 少,报错
49 #
50 # # x = 10
51 # # y = 20
52 # #
53 # # if x > y:
54 # #
55 # # print(x)
56 # #
57 # # else:
58 # # print(y)
59 #
60 # # 比较两数大小
61 # def max2(x, y):
62 #
63 # if x > y:
64 #
65 # print(x)
66 #
67 # else:
68 # print(y)
69 #
70 # max2(10, 30)
71 #
72 #
73 #
74 # # 空函数
75 # # 遇到一些比较难实现的功能,会导致暂时无法继续编写代码。
76 # # 所以一般在生产开发中,都会将所有功能实现定义成空函数。
77 # def func():
78 # pass # pass代表什么都不做
79
80
81
82 '''
83 函数的返回值
84 在调用函数时,需要接收函数体内部产生的结果,则return返回值。
85 '''
86 # def max2(x, y):
87 #
88 # if x > y:
89 #
90 # return x
91 #
92 # else:
93 #
94 # return y
95 #
96 # res = max2(10, 5)
97 #
98 # print(res)
99
100
101 '''
102 函数对象
103 指的是函数名指向的内存地址。
104 '''
105 # def func():
106 # pass
107 #
108 # # print(func) # <function func at 0x101dd2e18>
109 # #
110 # # func()
111 #
112 # def func2():
113 # pass
114 #
115 # # 把函数对象,传入字典中
116 # dict1 = {
117 # '1': func,
118 # '2': func2
119 # }
120 #
121 # choice = input('请输入功能编号:').strip()
122 #
123 # # if choice == '1':
124 # # func()
125 # # elif choice == '2':
126 # # func2()
127 #
128 # #
129 # # 若用户选择函数对象对应的key值,则调用该函数
130 # if choice in dict1:
131 # dict1[choice]() # dict1['1']
132
133
134 '''
135 函数嵌套:
136 嵌套定义:
137 在函数内,定义函数。
138
139 嵌套调用:
140
141 '''
142
143 # # 函数嵌套定义
144 #
145 # def func1():
146 #
147 # print('func1...')
148 #
149 # def func2():
150 # print('func2...')
151 #
152 # def func3():
153 # print('func3...')
154 #
155 # # ....
156 #
157 # return func3
158 #
159 # return func2
160 # # 通过函数内部的函数值,调用函数
161 # func2 = func1()
162 # func3 = func2()
163 # func3()
164 #
165 #
166 # # 函数嵌套调用
167 # def func1():
168 #
169 # print('func1...')
170 #
171 # def func2():
172 # print('func2...')
173 #
174 # def func3():
175 #
176 # print('func3...')
177 #
178 # # ....
179 #
180 # func3()
181 #
182 # func2()
183 #
184 # func1()
185
186
187
188 '''
189
190 '''
模块与包
1 # import 模块名 2 import B 3 4 # from 5 # 导入B模块中的a文件 6 # 会自动执行a文件中的代码 7 from B import a 8 9 # __name__: B.a 10 # a
day3作业
爬取梨视频
1 import requests
2 res = requests.get('https://video.pearvideo.com/mp4/adshort/20190613/cont-1565799-14012875_adpkg-ad_hd.mp4')
3
4 print(res.content)
5
6 with open('梨视频.mp4', 'wb') as f:
7 f.write(res.content)

浙公网安备 33010602011771号