运行必备库:pyperclip、BeautifulSoup、requests
完整代码(含注释)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
| import pyperclip import os import requests from bs4 import BeautifulSoup
def getHTMLText(url_home, code='utf-8'): try: r = requests.get(url_home) r.raise_for_status() r.encoding = code return r.text except: return ""
def getINFO(url_home, code='utf-8'): html = getHTMLText(url_home) soup = BeautifulSoup(html, 'html.parser') list0 = soup.find_all('div', attrs={'class': 'mdui-row mdui-col-sm-12'}) list1 = soup.find_all( 'div', attrs={'class': 'mdui-col-sm-3 mdui-text-right'}) list2 = soup.find_all( 'div', attrs={'class': 'mdui-col-sm-2 mdui-text-right'})
for list in list0[1:]: text = list.find('span').next_element.strip() FolderNames.append(text) for list in list1[1:]: text = list.next_element.strip() ModificationDates.append(text) for list in list2[1:]: text = list.next_element.strip() Sizes.append(text)
def PrintPanel(): width = int(70) Folder = int(30) Size = int(10) Date = width - Folder - Size header_fmt = '{{:{}}}{{:{}}}{{:>{}}}'.format(Folder, Size, Date) fmt = '{{:{}}}{{:>{}}}{{:>{}}}'.format(Folder, Size, Date)
print('=' * (width + 7)) print(header_fmt.format('Folder', ' Size', 'Date')) print('-' * (width + 7))
for i in range(len(FolderNames)): num = str(i).zfill(2) print('[', num, ']', fmt.format( FolderNames[i], Sizes[i], ModificationDates[i]))
print('=' * (width + 7))
FolderNames = [] Sizes = [] ModificationDates = []
url_home = 'https://brief-spurious-pizza.glitch.me/PicBed/Picture%20bed/'
Bool = 1 url_file = url_home
FolderTitle = ''
while Bool:
getINFO(url_file) PrintPanel() option = input("请选择目录/文件(+ 返回首页):")
try: url_add0 = str(FolderNames[int(option)]) url_file = url_file + url_add0 + '/'
if ("png") in url_file: url_md = '!' + '[' + url_add0 + ']' + '(' + url_file + ')' pyperclip.copy(url_md) print('文件URL已经复制到剪切板(MarkDown格式)') print('当前剪切板内容:' + url_file) url_file = url_file.replace( FolderNames[int(option)], '')[:-1] url_add0 = '' elif ("jpg") in url_file: url_md = '!' + '[' + url_add0 + ']' + '(' + url_file + ')' pyperclip.copy(url_md) print('文件URL已经复制到剪切板(MarkDown格式)') print('当前剪切板内容:' + url_file) url_file = url_file.replace( FolderNames[int(option)], '')[:-1] url_add0 = '' else: FolderTitle = FolderNames[int(option)]
except: if option == '+': url_file = url_home print("返回顶层".center(73, '-')) FolderTitle = '' else: print('=' * 77) print("输入异常".center(73, '-')) print('=' * 77)
os.system('pause') os.system('cls')
print(('Folder:' + FolderTitle).center(76, '='))
FolderNames.clear() Sizes.clear() ModificationDates.clear()
|