zoukankan      html  css  js  c++  java
  • python3获取指定网页,指定保存位置

    import os
    import requests

    def
    DownloadFile(url, savePath):
    #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")#

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

  • 相关阅读:
    html5 File api 上传案例
    DOM操作
    箭头函数
    js 高级函数
    导入导出封装
    函数
    哲学/文学
    qtMd5 加密算法
    生活感悟
    C# 小技巧
  • 原文地址:https://www.cnblogs.com/yanghao2008/p/10499018.html
Copyright © 2011-2022 走看看