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)
#如果实参和默认参数不同的话需要在实例化的时候写入实参

猜你喜欢

转载自blog.csdn.net/weixin_42218868/article/details/88545323