Python day_03

 

# 内置模块

# 模块与包
# 爬虫相关

内置模块

'''
time
json
os
sys

'''
# # time
# import time # 导入time模块
# # 获取时间戳
# print(time.time())
#
# # 等待两秒
# time.sleep(2)
# print(time.time())

# # os
# import os
# # 判断tank.txt是否在当前路径
# print(os.path.exists('tank.txt')) #False
# print(os.path.exists(r'D:\pyproject\day03\users.txt'))  # True
#
# #获取当前文件的根目录
# print(os.path.dirname(__file__))

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

# dumps:序列化
# 1、把字典转行成字符串
# 2、再把json数据转换成字符串
# res = json.dumps(user_info)
# print(res)
# print(type(res))

# # loads: 反序列化
# # json.loads()
# #1、把json文件的数据读到内存中
# with open('user.json', 'r', encoding='utf-8') as f:
#     #读取得到的是字符串
#     res = f.read()
#     # print(type(res))
#     #loads把json格式的字符串转换成dict类型
#     user_dict = json.loads(res)
#     print(user_dict) = json.loads(res)
#     print(type(user_dict))  # <class 'dict'>
#
# # dump
# user info = {}
#     'name': 'tank',
#     'pwd': '123'
# }
# with open('user_info.json', 'w', encoding='utf-8') as f:
#     # str1 = json.dumps(user_info)
#     # f.write(str1)
#     # json.dump(user_info, f)

 

02、模块与包 import +模块名

# import 模块名
import B

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

# __name__: B.a
# a

爬虫相关   以baidu.com和梨视频为例

''''''
'''
http协议:
    请求url:
        https://www.baidu.com/
        
    请求方式:
        GET
        
    请求头:
        Cookie: 可能需要关注。
        User-Agent: 用来证明你是浏览器
            注意: 去浏览器的request headers中查找
            Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36
        Host: www.baidu.com
'''

# requests模块使用
# pip3 install requests
# pip3 install -i 清华源地址 模块名
# 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)  # <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-1565852-14013295_adpkg-ad_hd.mp4')

print(res.content)
with open('vodio.mp4', 'wb') as f:
f.write(res.content)
 

猜你喜欢

转载自www.cnblogs.com/moxianyinzou/p/11022688.html