Python downloads network files to local

foreword

related introduction

  • Python is a cross-platform computer programming language. It is a high-level scripting language that combines interpretability, compilation, interactivity and object-oriented. Originally designed for writing automation scripts (shell), as the version is continuously updated and new language features are added, it is more and more used for the development of independent and large-scale projects.
  • PyTorch is a deep learning framework, which encapsulates many network and deep learning related tools for us to call, instead of writing them one by one. It is divided into CPU and GPU versions, and other frameworks include TensorFlow, Caffe, etc. PyTorch is launched by Facebook Artificial Intelligence Research Institute (FAIR) based on Torch. It is a Python-based sustainable computing package that provides two advanced features: 1. Tensor computing with powerful GPU acceleration (such as NumPy); 2. , Automatic differentiation mechanism when constructing deep neural network.
  • Requests is a popular Python third-party library for sending HTTP requests. It provides a simple and elegant way to interact with web services, including sending HTTP requests of types such as GET, POST, PUT, DELETE, and processing response data.
  • Here are some key features and usage of the Requests library:
    • Ease of use: The API design of the Requests library is very simple, making sending HTTP requests intuitive and easy to understand.
    • Multiple request methods: With functions such as requests.get(), requests.post(), requests.put(), requests.delete(), you can easily send different types of HTTP requests.
    • Request parameters: You can add query parameters, request headers, cookies, etc. to the request.
    • Request body: For POST and PUT requests, you can pass form data via the data parameter, or JSON data using the json parameter.
    • Response handling: The Requests library allows you to get response content in different formats (such as text, JSON, binary data), and provides convenient methods to handle response status codes, response headers, etc.
    • Exception handling: The Requests library will throw exceptions when request-related exceptions occur, such as connection timeout, request error, etc. You can use try and except to handle these exceptions.
  • Time is a module in the Python standard library for handling time-related operations. It provides a number of functions that enable you to get the current time, process time intervals, format time, and more.
  • Here are some key features and usage of the Time module:
    • Get the current time: You can use the time.time() function to get the number of seconds of the current time since January 1, 1970 (known as a Unix timestamp). This is useful for measuring time intervals, profiling, etc.
    • Formatting Time: With the time.strftime() function, you can format a time object as a string for display in a human-readable form. You can define the output format using a series of formatting directives (eg %Y for year, %m for month, etc.).
    • Parsing time: Using the time.strptime() function, you can parse a formatted time string into a time object.
    • Time delay: With the time.sleep() function, you can suspend the execution of the program for a specified number of seconds to achieve time delay.
    • Time measurement: You can use the time.perf_counter() and time.process_time() functions to measure the actual time of program execution and the processor time.

Python downloads network files to local

insert image description here

import requests
import time
 
def downloadFile(name, url):
    headers = {
    
    'Proxy-Connection':'keep-alive'}
    r = requests.get(url, stream=True, headers=headers)
    length = float(r.headers['content-length'])
    f = open(name, 'wb')
    count = 0
    count_tmp = 0
    time1 = time.time()
    for chunk in r.iter_content(chunk_size = 512):
        if chunk:
            f.write(chunk)
            count += len(chunk)
            if time.time() - time1 > 2:
                p = count / length * 100
                speed = (count - count_tmp) / 1024 / 1024 / 2
                count_tmp = count
                print(name + ': ' + formatFloat(p) + '%' + ' Speed: ' + formatFloat(speed) + 'M/S')
                time1 = time.time()
    f.close()
     
def formatFloat(num):
    return '{:.2f}'.format(num)
     
if __name__ == '__main__': 
    downloadFile('csdn.png', 'https://img-home.csdnimg.cn/images/20201124032511.png')

insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/FriendshipTang/article/details/132387410