python网络通信

import urllib#网络通信模块
from urllib import request
def downloader(url,isPicture=False):#默认参数
‘’’
:param url: 网址
:param isPicture:
:return: None,直接保存为文件,不需要返回值
‘’’
file_name = url.split("/")[-1]#切片1获得图片名称
response=urllib.request.urlopen(url)#resquest结果#response响应#urlopen网页地址urlopen网页地址

#查看请求响应
content=response.read()#response响应回答
if isPicture==True:#根据实参进行对应的的操作
    with open(file_name,"wb") as fb:#
        fb.write(content)
    print(content)
else:
    content = content.decode("utf-8")  # 对请求数据进行解码
    with open("index1.html","w",encoding="utf-8") as fb:
        fb.write(content)
    print(content)

downloader(“http://www.langlang2017.com/”)#默认参数在实参里可以写也可以不写
downloader(“http://www.langlang2017.com/img/banner1.png”,isPicture=True)
#如果实参和默认参数不同的话需要在实例化的时候写入实参

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值