lyj68

导航

 

作业(1):

从校花网爬取图片

# 从校花网爬取图片
import requests
res= requests.get('http://www.xiaohuar.com/d/file/20171202/3226be099ad8d610e92bbab5218047d1.jpg')
print(res.content)
with open('图片.jpg','wb') as f:
    f.write(res.content)

作业(2):

课堂笔记

# 昨日作业:登录功能
import os   # 与操作系统进行交互
def login():
    # 打开文件读取用户信息
    with open('user.txt','r',encoding='utf-8') as f:
        res=f.read()
        print(res)
        # 解压赋值
        username, password=res.split(',')

        # list1=res.split(',')
        # username=list1[0]
        # password=list1[1]
        # print(username,password)
    flag = True
    number = 0
    while flag:
        # 让用户输入用户名
        user = input('请输入用户名:').strip()
        if username == user:
            # 如果number=3,则表示输入密码错误超过或等于3次
            if number == 3:
                break
            while number <3:
                pwd = input('请输入密码:').strip()
                #
                if password == pwd:
                    print('登陆成功')
                    flag = False
                    break
                else:
                    print('登录失败')
                    number += 1
        else:
            print('用户名不存在,请重新输入!')

login()
# 无参函数
# 不需要接收外部传入的参数

# 有参数
# 需要接收外部传入的参数
def login(user, pwd):
    print(user, pwd)

# 传参多一或少一不可
login('lyj','123')

比较两数字大小
def max(x,y):
    if x>y:
        print(x)
    else:
        print(y)

max()

# 空函数
# 遇到一些比较难实现的功能,会导致暂时无法继续编写代码
# 所以一般在生产开发中,都会将所有功能实现定义成空函数
def func():
    pass  # pass代表什么也不做

'''
函数返回值
在调用函数时,需要接受函数内部产生的结果,则return
'''
def max(x, y):
    if x > y:
        return x
    else:
        return y

res = max()
print(res)

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

def fun2():
    pass
# 把函数对象,传入字典
dict1={'1':func,'2':fun2}

choice=input('请输入功能编号:').strip()
# if choice == '1':
#     func()
# if choice == '2':
#     fun2()
# 若用户选择函数对象对应的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()
#func2()
func3 = func2()
func3()

#函数嵌套调用
def func1():
    print('func1..')
    def func2():
        print('func2..')
        def func3():
            print('func3..')
        func3()
    func2()
func1()
'''
函数参数:
    函数定义:
    def 函数(函数形参)
'''

'''
名称空间
python解释器自带的:内置名称空间
自定义的py文件内,顶着最左边定义:全局名称空间
函数内部定义:局部名称空间
'''
name = 'lyj'
def func1():
    # name = 'lyj'
    print(name)
    def func2():
        print('func2..')

print(name,'全局打印')
func1()

#import 模块名
import B

# # from 03 import a
#导入B模块中的a文件
# 会自动执行a文件中的代码
from B import a
# __name__=B.a
a
'''
time
json
os
sys
'''

# time
import time    # 导入time模块
# 获取时间戳
print(time.time())

# 等待
time.sleep(2)   # 等待10秒
print(time.time())

# os
import os
# 可以与操作系统中的文件进行交互
# 判断user.txt文件是否存在
print(os.path.exists('user.txt'))   #True
print(os.path.exists(r'D:\python学习\user.txt'))   #True

# 获取当前文件的根目录
print(os.path.dirname(__file__))   # D:/python学习

# sys
# import sys
# # 获取python在环境变量中的文件路径
print(sys.path)
sys.path.append(os.path.dirname(__file__))
print(sys.path)

#json
import json
user_info = {
    'name' : 'lyj',
    'pwd' : '123'
}

# dumps:序列化
# 1.把字典转型成json数据
# 1.再把json数据转换成字符串
res = json.dumps(user_info)
print(res)
print(type(res))
with open('user.json','wt',encoding='utf-8') as f:
    f.write(res)


# 1.把json文件的数据读到内存中
with open('user.json','r',encoding='utf-8') as f:
    res = f.read()# loads :反序列化
# json.loads()
    # print(type(res))
    user_dict = json.loads(res)
    print(user_dict)
    print(type(user_dict))    # <class 'dict'>


# dump
user_info = {
    'name' : 'lyj',
    'pwd' : '123'
}
import json
with open('user.json','wt',encoding='utf-8') as f:
    # dump:自带write功能
    json.dump(user_info,f)

# load
# load: 自动触发f.read()
with open('user.json','rt',encoding='utf-8') as f:
    user_dict = json.load(f)
    print(user_dict)
'''
http:
    请求url:
        https://www.baidu.com/
    请求方式:
        GET
    请求头:
    Cookie:可能需要关注
    User-Agent: (用来证明你是浏览器)
        Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36
    Host:www.baidu.com(有时会对其进行反爬)

'''

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

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

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('shipin.mp4','wb') as f:
    f.write(res.content)

 

小结:今天初步接触了爬虫,很好玩,很有意思

 

posted on 2019-06-13 18:08  lyj68  阅读(79)  评论(0)    收藏  举报