Learn python the third day.

# 今日内容:
# 函数剩余部分
# 内置模块
# 模块与包

1 函数的三种定义方式无参函数
不需要接收外部传入的参数
def foo():
print('from foo..')
foo()

有参函数
需要接收外部传入的参数
def login(user,pwd)
print(user)
传参多一或少一不可

login('cc','123')
login('cc','123','111') # 多,报错
login('cc') # 少,报错

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)
'''


'''
2 # 函数对象
# 指的是函数名指向的内存地址
def func():
pass
print(func)
func()

def func2():
pass
dict1={
'1':func,
'2':func2
}

choice=input('请输入功能编号:').strip()

# if choice=='1':
# func()
# else choice=='2':
# func2()

#1
# 若让用户选择函数对象对应的key值,则调用该函数
if choice in dict1:
dict1[choice]() # dict1['1']




'''
3 函数嵌套:
嵌套定义:在函数内,定义函数
嵌套调用:

# 函数嵌套定义
def func1():
print('func1...')
def func2():
print('func2...')
def 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()

# 4.名称空间
# python 解释器自带的:内置名称空间
# 自定义的py文件内,顶着最左边定义的:全局名称空间
# 函数内部定义的:局部名称空间

name='cc'
def func1():
print(name)

def func2():...
print('func2...')
#print(name,'全局变量')

func1()

# 模块与包

# import 模块名
# import B
# from
# 导入B模块中的文件a
# 会自动执行a文件中的代码

# from B import a
# __name__:B.a
# a

a.py
# print('from a')
def func1():
print('from func1')

print(__name__) # main

# 用于测试函数
if __name__=='__mian__':
func1()


# 常用模块(内置模块)
# time
# json
# os
# sys



# 1 # time
# import time      # 导入时间模块
# # 获取时间戳
# print (time.time())
# # 等待2s
# time.sleep(2)
# print(time.time())
#
#
# 2 # os
# import os
# # 判断cc.txt文件是否存在
# print(os.path.exist('cc.txt'))  # True
# print(os.path.exist('cc1.txt'))   # False
# print(os.path.exist(r'...\cc.txt'))
#
# # 接收当前文件的根目录
# print(os.path.dirname(__file__))
#
#
#
# 3 # sys
# import sys
# # 获取python在环境变量中的文件路径
# print(sys.path)
# # 把项目的根目录添加到环境变量中
# sys.path.append(os.path.dirname(__file__))
# print(sys.path)


# 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)



4 # json
import json
user_info={
    'name':'cc',
    'pwd':'123'

}


res=json.dumps(user_info)
print(res)
print(type(res))   #结果:变成双引号,字符串


with open('user.json','wt',encoding='utf-8') as f:
    f.write(res)


# lodes:反序列化
# json.lodes()
# 1 把json 文件的数据读到内存中

with open('user.json','wt',encoding='utf-8') as f:

    # 读到得到的是字符串
    res=f.read()

    # print(type(user_dict))   # <class
    # lodes把json格式的字符串转换成dict 类型

    user_dict=json.lodes(res)
    print(user_dict)  # 'name':'cc','pwd':'123'
    print(type(user_dict))   # <class 'dict'>



# dump
user_info={
    'name':'cc',
    'pwd':'123'

}
with open('user.json','wt',encoding='utf-8') as f:
    # str1=json.dump(user_info)
    # f.write()

    json.dump(user_info,f)    # dump 自带write功能




# lode
with open('user.json','wt',encoding='utf-8') as f:
    # res=f.read()
    # user_dict=json.lodes(res)
    # print(user_dict)

    # lode:自动触发f.read()
    user_dict=json.lode(f)
    print(user_dict)

'''
爬虫
只提取想要的有价值的数据
'''

'''
http协议:
请求URL:
http://www.baidu.com/
请求方式:
get
请求头:
cookie:可能需要关注
User-Agent:用来证明你是浏览器
注意:去浏览器的request header 中查找
Mozilla/5.0(windows NT 10.0.....)
Host: www.baidu.com
'''

# requests模块使用
# pip3 install requests
# pip3 install -i 清华源地址 模块名
# pip3 install -i http://

import requests

response=requests.get(url='http://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)



 

猜你喜欢

转载自www.cnblogs.com/feiyufei/p/11014544.html