Common Python toolkits (more on this later)

Tip: If you get an error after importing a package, check for indentation. Python has a strict indentation specification

Utility class

  • Images are downloaded
  • File is written to
  • Export data
"" "Descripte: Tool kit relatedDate: September 14, 2020 16:28:51Author: xiongchao"" "
import os import threading import json import requests from openpyxl import Workbook   class pyUtils:   "" "Function description: picture download Parameters:  urlList :[  { url:"http://example.com/img/url/download... ',Name :" Image name } ].Path: indicates the download path Returns: File: Downloads the file to the specified path"" "   def download(url, name, path):  try:  print('Start downloading :', name)  content = requests.get(url).content  path = '%s/%s.jpg' % (path, name)  with open(path, 'wb') as f:  f.write(content)  print('Download completed', name)  except:  print("Error: system error,please try again later ")   def img_download(urlList, path):  if path:  if not os.path.exists(path):  os.mkdir(path)  print("Folder {} created successfully".format(path))   if urlList:  for list in urlList:  print("Download address is", list['url'])  threading.Thread(target=pyUtils.download, args=(  list['url'], list['name'], path)).start()   "" "Function description: text write Parameters: Name: the titlePath: indicates the save pathText: The problem content to be written Returns: File: Downloads the file to the specified path"" "  def write(name, path, text):  The second parameter, data A, identifies appending writes  try:  if not text:  with open(path, 'a', encoding='utf-8') as f:  f.write(name + '\n')  f.writelines(text)  f.write('\n\n')  print("File {}. TXT, done".format(print))  except:  print("Data write failure")   "" "Function description: Data export Parameters: Name: file name + save path.. /demo/ File export XLSXHeader: headerContent: data"" "  def excel_import(name, header, content):  Create a file object  wb = Workbook()  Create a new table  ws = wb.active  Set the table header  if header:  for row in range(len(header)):  c = row + 1  ws.cell(row=1, column=c, value=header[row])  else:  print("The header is empty")  Fill in the form  if content:   for listIndex in range(len(content)):  ws.append(content[listIndex])   wb.save(filename=data_fileName)  print("Write succeeded")  else:  print("No data.")    Copy the code

test

# Import your own toolkit
from pyUtils import pyUtils

urlList = [
    {'url': 'https://www.logosc.cn/uploads/icon/2019/06/14//54538bb3-706b-40f6-bc20-63a5350b4909.png'.'name': 'Flame logo image.png'}, 
 {'url': 'https://www.logosc.cn/uploads/icon/2019/06/14//8a748e3a-faa9-4c8e-bbdb-3c535cdc60d3.png'.'name': 'Music Line logo.png'},  {'url': 'https://www.logosc.cn/uploads/icon/2019/06/14//7f3daa30-da19-46c4-9591-cd3b73205b7d.png'.'name': 'Wolf Roar logo icon'},  {'url': 'https://www.logosc.cn/uploads/icon/2019/06/14//a2fecd51-1b09-47d6-b797-3e71cbdd4a4a.png'.'name': 'Music symbol logo icon.png'} ]  pyUtils.img_download(urlList,"Download")    Copy the code



This article is formatted using MDNICE