python作业6月13日

# 昨日作业:
# 登陆功能
# import os 操作系统
# def login():
 
# 今日内容:
# 函数剩余部分
# 内置模块
# 模块与包
# 爬虫基本原理
# 函数定义方式
# 无参函数
# def foo():
# foo()
 
# 有参函数
# def login(user,pwd):

# login(user,pwd)
# 空函数
# pass
# pass代表都不做
# 返回值 return
# 嵌套定义
# 函数内定义 调用
# 嵌套调用
# 通过函数内部返回值调用函数
 
# def fun1():
# pass
# def fun2():
# pass
# fun2()
# fun1()
# import a
# from b import a
# 常用模块 内置模块
# import time
# print(time.time())
# import os
# os.path.exists("")
import json
# user_info={
# 'name':'tank',
# 'pwd':'123'
# }
# dumps 序列化
# res=json.dumps(user_info)
# print(res)
# print(type(res))
# loads反序列化
# with open('user.json','r',encoding='utf-8') as f:
# # res=f.read()
# # user_dict=json.loads(res)
# # print(user_dict)
# # print(type(user_dict))
# json.dump(user_info,f)自带w功能
# with open ('user_info.json','r',encoding='utf-8') as f:
# res=f.read()
# user_dict=json.loads(res)
# user_dict=json.load(f)
# 接下来学习爬虫
# http协议 请求url
# http://www.baidu.com/
# 请求方式:get
# 请求头
# cookie:可能需要关注
# user-agent:用来证明是浏览器
import requests
import os
# response=requests.get(url='http://www.baidu.com/')
# print(response)
# print(response.status_code)
# print(response.text)
# with open('baidu.html','w',encoding='utf-8') as f :
# f.write(response.text)
response=requests.get('http://www.xiaohuar.com/p-3-126.html')
with open('shiping.mp4','wb') as f:
f.write(response.content)

爬虫

复制代码
# #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)
复制代码
posted @ 2019-06-20 22:09  骧骧骧骧  阅读(151)  评论(0编辑  收藏  举报