### [wk15_1214_B1003217] ### [Inclass practice] #### Line sticker doenlown 1. 課本原始檔 2. 影音教學 step by step 3. stickershop 貼圖完整 4. emojishop 表情貼 5. chatGpt - local jpg 6. chatGpt - line png ```python ## 課本原始檔 import requests, json, os from bs4 import BeautifulSoup url = 'https://store.line.me/stickershop/product/30084/zh-Hant' #人的日常 html = requests.get(url) sp = BeautifulSoup(html.text, 'html.parser') #datas = sp.find_all('li', {'class':'mdCMN09Li FnStickerPreviewItem'}) datas = sp.find_all('li', {'class':'mdCMN09Li FnStickerPreviewItem static-sticker'}) # 建立目錄儲存圖片 images_dir= "line_image/" if not os.path.exists(images_dir): os.mkdir(images_dir) for data in datas: imginfo = json.loads(data.get('data-preview')) id = imginfo['id'] imgfile = requests.get(imginfo['staticUrl']) full_path = images_dir + id + '.png' with open(full_path, 'wb') as f: f.write(imgfile.content) print(full_path) ```python #lineSticker 教學-1 import requests, json, os from bs4 import BeautifulSoup ``` ```python #lineSticker 教學-2 url = 'https://store.line.me/stickershop/product/8991459/zh-Hant' html = requests.get(url) html ``` <Response [200]> ```python #lineSticker 教學-3 html.text ``` ```python #lineSticker 教學-4 sp = BeautifulSoup(html.text, 'html.parser') sp ``` ``python #lineSticker 教學-5 sp.title.text ``` ```python #lineSticker 教學-6 sp.find_all('li', {'class':'mdCMN09Li FnStickerPreviewItem static-sticker'}) ``` ```python #lineSticker 教學-7 sp = BeautifulSoup(html.text, 'html.parser') datas = sp.find_all('li', {'class':'mdCMN09Li FnStickerPreviewItem static-sticker'}) print(datas) ``` ```python #lineSticker 教學-8 len(datas) ``` 32 ```python #lineSticker 教學-9 for data in datas: print(data.get('data-preview')) ```python #lineSticker 教學-10 for data in datas: imginfo = json.loads(data.get('data-preview')) print(imginfo['id']) print(imginfo['staticUrl']) ``` ```python #lineSticker 教學-11 imges_dir = "line_image/" if not os.path.exists(imges_dir): os.mkdir(imges_dir) for data in datas: imginfo = json.loads(data.get('data-preview')) id = imginfo['id'] imgfile = requests.get(imginfo['staticUrl']) full_path = imges_dir + id + '.png' with open(full_path, 'wb') as f: f.write(imgfile.content) print(full_path) ``` ```python #stickershop 完整 import requests, json, os from bs4 import BeautifulSoup #url = 'https://store.line.me/stickershop/product/8991459/zh-Hant' #人的日常 url = 'https://store.line.me/stickershop/product/24885624/zh-Hant' #喵咪跟兔嘰 html = requests.get(url) sp = BeautifulSoup(html.text, 'html.parser') datas = sp.find_all('li', {'class':'mdCMN09Li FnStickerPreviewItem static-sticker'}) imges_dir = "line_image/" if not os.path.exists(imges_dir): os.mkdir(imges_dir) for data in datas: imginfo = json.loads(data.get('data-preview')) id = imginfo['id'] imgfile = requests.get(imginfo['staticUrl']) full_path = imges_dir + id + '.png' with open(full_path, 'wb') as f: f.write(imgfile.content) print(full_path) ``` ```python #表情貼-1 import requests, json, os from bs4 import BeautifulSoup #url = 'https://store.line.me/stickershop/product/8991459/zh-Hant' #人的日常 #url = 'https://store.line.me/stickershop/product/24885624/zh-Hant' #喵咪跟兔嘰 url = 'https://store.line.me/emojishop/product/646b23ff903b5543729ffa4d/zh-Hant' #超實用❤生活用語對話框 #url = 'https://store.line.me/emojishop/product/61b04cbaf864dd7f3763ab8b/zh-Hant' #超實用❤手勢/符號 動態表情貼 html = requests.get(url) html ``` ```python #表情貼-2 sp = BeautifulSoup(html.text, 'html.parser') #datas = sp.find_all('li', {'class':'mdCMN09Li FnStickerPreviewItem static-sticker'}) #datas = sp.find_all('li', {'class':'mdCMN09Li FnStickerPreviewItem'}) datas = sp.find_all('li', {'class':'mdCMN09Li FnStickerPreviewItem'}) print(datas) ``` ```python #表情貼-3 imges_dir = "line_image_emojishop/生活用語對話框/" if not os.path.exists(imges_dir): os.mkdir(imges_dir) for data in datas: imginfo = json.loads(data.get('data-preview')) id = imginfo['id'] imgfile = requests.get(imginfo['staticUrl']) full_path = imges_dir + id + '.png' with open(full_path, 'wb') as f: f.write(imgfile.content) print(full_path) ``` ```python ## chatGpt local jpg import shutil source_path = r'C:\zzz\111.jpg' destination_path = r'C:\zzz\2.jpg' # 複製文件 shutil.copyfile(source_path, destination_path) print(f'已從 {source_path} 複製到 {destination_path}') ``` ```python ## chatGpt line png import requests import os url = 'https://stickershop.line-scdn.net/stickershop/v1/sticker/230054638/android/sticker.png?v=1' save_path = r'C:\zzz\3.jpg' #response = requests.get(url, stream=True) response = requests.get(url) with open(save_path, 'wb') as file: for chunk in response.iter_content(chunk_size=8192): file.write(chunk) print(f'已從 {url} 下載並保存到 {save_path}') ``` ### 凱薩密碼 參考文件: https://github.com/CrypTools/CaesarCipher ```python import string import matplotlib.pyplot as plt LETTERS = 'abcdefghijklmnopqrstuvwxyz,.() ' def encrypt(initial, shift): initial = initial.lower() output = "" for char in initial: if char in LETTERS: output += LETTERS[(LETTERS.index(char) + shift) % len(LETTERS)] return output def decrypt(initial, shift): initial = initial.lower() output = "" for char in initial: if char in LETTERS: output += LETTERS[(LETTERS.index(char) - shift) % len(LETTERS)] return output def plot_letter_frequency(text): text = text.lower() letter_count = {letter: 0 for letter in string.ascii_lowercase} for char in text: if char.isalpha(): letter_count[char] += 1 letters = list(letter_count.keys()) counts = list(letter_count.values()) plt.bar(letters, counts) plt.xlabel('Letters') plt.ylabel('Frequency') plt.title('Letter Frequency in Text') plt.savefig('./result.png') ## --------------- Main ------------------ message = input('請輸入要加密的訊息: ') key = int(input('請輸入密鑰: ')) # 加密 cipher = encrypt(message, key) print(f'\n加密結果: {cipher}') # 解密 plain = decrypt(cipher, key) print(f'\n解密結果: {plain}') #字數統計 plot_letter_frequency(cipher) ``` 請輸入要加密的訊息: Teddy 請輸入密鑰: 4523 加密結果: qbaav 解密結果: teddy ![png](output_22_1.png) ![result](https://hackmd.io/_uploads/Hk2ZhxuIa.png) ### [Afterclass practice] <pre> 1.綜合演練 選擇題1-10 2.教學影音 lesson 10 3.請填寫小組報告主題 --- 【期末小組】管理 4.請填寫筆記網站網址 --- 【OMP】問卷 </pre> #### 綜合演練 <pre> A 1.以open(filename[,mode][,encode]) 開啟檔案,下列何者是mode參數預設的模式? (A) 讀取模式 (B) 寫入模式 (C) 附加模式 (D)以上皆是 C 2.Python提供何種內建函式,可以開啟指定的檔案,以便進行檔案內容的讀取、寫入或修改? (A) file() (B) input() (C) open() (D) output() B 3.下列何函式可以讀取一列字元? (A) readable() (B) read() (C) readlines() (D)get(ch) B 4.下列程式建立的檔案物件,可以執行何種動作? f=open('file1.txt','w') f.write("Hello Python!") f.close() (A) 讀取 (B) 寫入 (C) 可讀取也可寫入 (D) 以上皆非 D 5. 執行下列程式,下列顯示結果何者正確? try: print(x) except: print("y") finally: print("z") (A) x (B) y (C) xz (D) yz A 6.open(filename,mode,encode) 函式的參數中,其中只有那一個參數是必填? (A) filename (B) mode (C) encode (D) 以上皆是 B 7.如果作業系統是繁體中文Windows系統,預設的編碼為何? (A) UTF-8 (B) cp950 (C) unicode (D) GB2312 D 8.下列有關 readlines()的敘述,何者正確? (A) 會讀取全部文件內容 (B) 以串列方式傳回 (C) 包括「\n」跳列字元,甚至是隱含的字元 (D) 以上皆是 A 9. 執行下列程式,下列顯示結果何者正確? n=1 try: print(n) except: print("變數不存在!") (A) 1 (B) n (C) 變數不存在 (D) 以上皆是 C 10.班上的除錯高手大匹,在他的程式中加入了錯誤的補捉在 try…except…finally 敘述中,無論例外有沒有發生都會執行下列那些程式區塊? (A) try (B) except (C) finally (D) 以上皆是 </pre> ```python #5 try: print(x) except: print("y") finally: print("z") ``` y z ```python #9 n=1 try: print(n) except: print("變數不存在!") ``` 1 #### 教學影音lesson10 <pre> 例外處理: try...except...else...finally 語法: try: 程式碼A except[例外狀況]: 程式碼B ... else: 程式碼C finally: 程式碼D 說明: 程式碼A:執行進行例外偵測 程式碼B:若有發生例外時執行 程式碼C:若沒有發生例外時執行 程式碼D一定會例外 except:設定多種例外 else、finally:非必填 </pre> ### [Self practice] ```python #開啟一個檔案並讀取其中的內容 file_path = "example.txt" try: # 嘗試開啟檔案 with open(file_path, 'r') as file: content = file.read() # 這裡可能會發生IOError或UnicodeDecodeError except FileNotFoundError: print(f"錯誤:找不到檔案 {file_path}") except (IOError, UnicodeDecodeError) as e: print(f"錯誤:無法讀取檔案 {file_path},原因:{e}") else: # 如果沒有發生例外,執行這個區塊 print(f"成功讀取檔案 {file_path}") finally: # 無論是否發生例外,都會執行這個區塊 print("結束處理檔案") # 這裡可以放置其他程式碼,不受 try-except 區塊的影響 ``` 錯誤:找不到檔案 example.txt 結束處理檔案