代码改变世界

python第三课

2019-06-13 18:19  a_mf  阅读(218)  评论(0)    收藏  举报
今日内容:
函数剩余部分
内置模块
模块与包
爬虫基本原理
requests模块
函数
#函数的三种定义方式
#无参数函数(不需要接收外部传入的参数# )
# def foo():
#     print('hellow')
# foo()
# #有参函数(需要接收外部传入的参数)
# def login(user,pwd):
#     print(user,pwd)
# login('limengfan','123')  #参数缺一不可,少一不可
# #空函数
# def fun():
#     pass



#函数返回值
#在调用函数是,需要接收函数体内产生的结果,用return返回
# def max(x,y):
#     if x>y:
#         return(x)
#         #print(x)
#     else:
#         return (y)
#        #print(y)
# res=max(10,20)
# print(res)


#函数对象(指函数名指向的的内存地址)
# def fun():
#     pass
# # print(fun)
# # fun()
# def fun1():
#     pass
# dict1={
#     '1':fun
#     '2':fun1
# }

# choice=input('请输入你想要实现的功能:').strip()

#普通编写
# if choice == '1'
#     fun()
# elif choice == '2':
#     fun1()
#特殊
# if choice in dict1:
#     dict1[choice]()

#函数嵌套
#嵌套定义:在函数内定义函数
# def fun2():
#     print('fun2....')
#     def fun3():
#     print('fun3....')
#         def fun4():
#             print('fun4....')
#         return fun4
#      return fun3
# #函数嵌套的调用
#通过函数的内部的函数值,调用函数
# fun3=fun2()
# fun4=fun3()
# fun4()



# def fun2():
#     print('fun2....')
#     def fun3():
#          print('fun3....')
#          def fun4():
#             print('fun4....')
#          fun4()
#     fun3()
# fun2()
#


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

name='limengfan'
def fun1():
    print(name)
    def fun2():
        print('fun2....')
# fun2()
print('全局定义',name)
fun1()
内置函数
# # #time
# # import time  #导入time模块
# # #获取时间戳
# # print(time.time())
# # #等待
# # time.sleep(2)
# # print(time.time())
#
#
# #os
#
# import os  #与操作系统中的文件进行交互
# print(os.path.exists('li.txt'))
#
#
#
# #获取当前文件的根目录
# print(os.path.dirname(__file__))
#
#
# #sys
# import sys
# #获取python的环境变量中的文件路径
#
# sys.path.append(os.path.dirname(__file__))  #把项目的根目录添加到环境变量中去
# print(sys.path)
#
#
#
# #json
# import json
# user_info={
#     'name':'limengfan',
#     'pwd':'123'
# }
# #dump :序列化 (把字典转换成json数据,再把json数据转换成字符串)
# res = json.dumps(user_info)
# with open('li.json','r',enconding='utf-8') as f:
#     f.write(res)
#
#
# # loads:反序列化(1、把json文件数据读到内存中)
# import json
# with open('li.json','r',encoding='utf-8') as f:
#     res = f.read()#字符串
#     user_dict1=json.loads(res)  #loads把json格式转换成dict类型
#     print(user_dict1)
#
#
# #dumps
# import json
# user_info={
#      'name':'limengfan',
#      'pwd':'123'
#  }
# with open('user_info.json','w',encoding='utf-8') as f:
#     json.dump(user_info,f)#dunmp:自带write功能
#
# with open('user_info.json','r',encoding='utf-8') as f:
#     user_dict1=json.load(f)#load:自动触发f.read()
#     print(user_dict1)

爬虫

# #http协议
#      请求URL:
#      请求方式
#      请求头:
#          Cookie:可能需要关注(本地用户信息)
#           User-agent:(判断是否为浏览器) 用来证明你是浏览器
#           host:当前访问的浏览器的域名

#request
import requests     #pip3 install requests
                    #pip3 install -i 清华源地址  模块名



import requests
res = requests.get(url='地址')  #获取地址给你的返回信息
res.encoding='utf-8'  #将获取的返回信息进行转码
print(res.status_code#/获取返回信息的对象状态码
print(res.test)#返回相应文本

with open ('baidu.html','w',encoding='utf-8') as f:
    f.write(res.test)   #将获得的文本写入到baidu.html文件中



import requests
res=requests.get("dizhi"))
print(res.content)#图片和视频都是二进制流,需要用content
with open('ship.mp4','wb') as f:
    f.write(res.content)

作业三:

import requests
res=requests.get("http://www.xiaohuar.com//d/file/20151125/b61118414b8409cc22d037bff1f8fcd4.jpg')")
print(res.content)
with open('li.jpg','wb') as f:
    f.write(res.content)

 

 

小结:接下来更加要多交一点爬东西的东西吧