Python3下载文件或图片方法
1、使用requests
importos
importrequests
defdownload_file(url,store_path):
filename=url.split("/")[-1]
filepath=os.path.join(store_path,filename)
file_data=requests.get(url,allow_redirects=True).content
withopen(filepath,'wb')ashandler:
handler.write(file_data)
2、使用urllib.request.urlretrieve
importos
fromurllib.requestimporturlretrieve
defdownload_file(url,store_path):
filename=url.split("/")[-1]
filepath=os.path.join(store_path,filename)
urlretrieve(url,filepath)