SoFunction
Updated on 2024-11-17

python3 get file url content and download code example

This article introduces the python3 to get the file url content and download code examples, the text of the sample code through the introduction of the very detailed, for everyone's learning or work has a certain reference value of learning, the need for friends can refer to the next!

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time  : 2019-12-25 11:33
# @Author : Anthony
# @Email  : ianghont7@
# @File  : get_video_audio_file.py

import xlrd as xl
import requests



# Create folders
def mkdir_floder(path):
  import os
  isExists = (path)
  if not isExists:
    (path)
    # print(path + ' Created successfully')
    return True
  else:
    # If the directory exists it is not created and prompts that the directory already exists
    # print(path + ' Directory already exists')
    return False



def request_floder(floder01,floder02,filename,url,types):
  # Splice the full path
  all_path = basedirpath+floder01+'/'+floder02+'/'
  res = (())
  music = 
  with open(all_path+filename+'.'+types, 'ab') as file: # Name of file saved locally
    (music)
    ()



def get_xls(filename):
  # Open the file
  xls_file=xl.open_workbook(filename)
  # Get the contents of the first sheet
  xls_sheet=xls_file.sheets()[0]
  # of total rows
  line_nums = xls_sheet.nrows
  for i in range(0,line_nums):
    # Get the contents of each line
    row_value = xls_sheet.row_values(i)
    # Get first level directory name
    folder1 = row_value[0]
    # Get the name of the second level directory
    folder2 = row_value[1]
    # Get filename
    file_name = row_value[2]
    # Get url content
    url = row_value[3]
    # Create directories
    mkdir_floder(basedirpath+folder1+'/'+folder2)
    # Convert the type of the url to dict
    url = eval(url)

    # Access to video content
    if "audio" in url:
      if url["audioF"].strip() != "":
        audio_url = url["audioF"]
        audio_url_end = audio_url.split('.')[-1]
        print(audio_url)
        request_floder(folder1, folder2, file_name, audio_url, audio_url_end)

      elif url["audio"].strip() != "":
        audio_url = url["audio"]
        audio_url_end = audio_url.split('.')[-1]
        print(audio_url)
        request_floder(folder1, folder2, file_name, audio_url, audio_url_end)

    # Getting audio content
    elif "video" in url:
      if url["video"].strip() != "":
        video_file = url["video"]
        video_file_end = video_file.split('.')[-1]
        print(video_file)
        request_floder(folder1, folder2, file_name, video_file, video_file_end)

if __name__ == "__main__":
  # File storage path
  basedirpath = "/Users/ianthony/Desktop/Devops/"
  # xlsx files read
  get_xls("")

This is the whole content of this article.