python3获取指定网页,指定保存位置

Posted 重积德

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python3获取指定网页,指定保存位置相关的知识,希望对你有一定的参考价值。

import os
import request

def
DownloadFile(url, savePath): if not os.path.exists(savePath): print("downloading {0}".format(url)) kv={user-agent:Mozilla/5.0} try: r = requests.get(url,headers=kv, timeout=60) r.raise_for_status() # 如果响应状态码不是 200,就主动抛出异常 with open(savePath,wb)as f: f.write(r.content) f.close() print("save file succeed") time.sleep(1) except requests.RequestException as e: print(e) with open(D:/timeout.txt, a+,encoding = UTF-8) as fw: #读入存储文件路径 fw.write(%s,%s % (url,e)) else: print("file already exists")#

 

一个简单的下载,并指定保存位置的函数

以上是关于python3获取指定网页,指定保存位置的主要内容,如果未能解决你的问题,请参考以下文章