Day03,爬虫的一天
定义函数的三种方式:
# # 无参函数
# # 不需要接收外部传入的参数
# 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)
#
#
#
# # 空函数
# # 遇到一些比较难实现的功能,会导致暂时无法继续编写代码。
# # 所以一般在生产开发中,都会将所有功能实现定义成空函数。
# def func():
# pass # pass代表什么都不做
函数的返回值:
'''
函数的返回值
在调用函数时,需要接收函数体内部产生的结果,则return返回值。
'''
def max2(x, y):
if x > y:
return x
else:
return y
res = max2(10, 5)
print(res)
函数对象:
'''
函数对象
指的是函数名指向的内存地址。
'''
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']
函数的嵌套:
'''
函数嵌套:
嵌套定义:
在函数内,定义函数。
嵌套调用:
'''
# # 函数嵌套定义
#
# 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 = 'tank'
def func1():
# name = 'tank'
print()
def func2():
print('func2...')
# print(name, '全局打印')
func1()
模块与包:
# import 模块名 import B # from #导入B模块中的a文件 #会自动执行a文件中的代码 from B import a # __name__:B.a # a
爬虫:
'''
http协议:
请求url:
url=https://www.baidu.com/
请求方式:
GET
请求头:
Cookie:BAIDUID=16809FFBED4845538426C690772DB48C:FG=1; BIDUPSID=16809FFBED4845538426C690772DB48C; PSTM=1549625281; BD_UPN=12314753; BDUSS=czMlZoN3ljRzlFYTlKak5LNDI5UVlCUzliM3k3SkNXQTY0bHRsbnZNUVhKc3hjQVFBQUFBJCQAAAAAAAAAAAEAAAA0SqSiAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABeZpFwXmaRcNn; ispeed_lsm=2; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; COOKIE_SESSION=7_1_8_7_2_4_0_3_8_3_0_0_63_0_303_338_1560569545_1560569510_1560569848%7C9%2352793_30_1560569848%7C3; BD_HOME=1; H_PS_PSSID=1422_21125_29135_29237_28518_29098_29369_28837_29220_20718
User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Mobile Safari/537.36
Host: www.baidu.com
'''
# import requests
#
# 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)
#
# 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/20190615/cont-1566634-14020800_adpkg-ad_sd.mp4')
print(res.content)
with open('视频.mp4','wb') as f :
f.write(res.content)
# import requests
# res = requests.get('https://video.pearvideo.com/mp4/adshort/20190613/cont-1565846-14013215_adpkg-ad_hd.mp4')
#
# print(res.content)
#
# with open('视频1.mp4', 'wb') as f:
# f.write(res.content)
总结:
今天是爬虫的一天,因为我非常想爬爱奇艺VIP视频和付费视频。最终的结果是我不会搞,😫,不过在我不断询问度娘的同时,也学到了不少知识。星期六上午考的四级,下午尝试爬爱奇艺VIP。晚上打的篮球,吃的烧烤和同学侃侃未来。周日玩了一天,以后还是规定只周六下午打球,晚上打游戏或看电影啥的。周日还是去学习比较好,好像把这里当作日记本了,嘻嘻。这一个月要6门考试加上这个实习,要累死!加油吧,少年!
每天给自己打个气,未来加油Fighting!!!冲冲冲!!!
生存还是毁灭,这是个问题!

浙公网安备 33010602011771号