## wk15_1214_lineStickerDownload 1. 課本原始檔 2. 影音教學 step by step 3. stickershop 貼圖完整 4. emojishop 表情貼 5. chatGpt - local jpg 6. chatGpt - line png ### 【Inclass practice】 ```python ## 課本原始檔 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/16897/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'}) # 建立目錄儲存圖片 images_dir= "line_1/" 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 ``` ```python #lineSticker 教學-3 html.text ``` ```python #lineSticker 教學-4 sp = BeautifulSoup(html.text, 'html.parser') sp ``` ```python #lineSticker 教學-5 sp.title.text sp.li.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) ``` ```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\2.jpg' destination_path = r'C:\zzz\22.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/293948094/iPhone/base/plus/sticker@2x.png?v=9' save_path = r'C:\zzz\333.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}') ``` ### 【afterclass practice】 1. 綜合演練 選擇題1-10 (需抄題在markdown cell ; 有程式碼的題目要有code cell ) 2. 教學影音 lesson 10 3. 請填寫小組報告主題 --- 【期末小組】管理 4. 請填寫筆記網站網址 --- 【OMP】問卷 ```python 1. 以open(filename[,mode][,encode]) 開啟檔案,下列何者是 mode 參數預設的模式? (A) 讀取模式 (B) 寫入模式 (C) 附加模式 (D)以上皆是 ``` ```python ANS:( A ) ``` ```python 2. Python 提供何種內建函式,可以開啟指定的檔案,以便進行檔案內容的讀取、寫入或修改? (A) file() (B) input() (C) open() (D) output() ``` ```python ANS:( C ) ``` ```python 3. 下列何函式可以讀取一列字元? (A) readable() (B) read() (C) readlines() (D)get(ch) ``` ```python ANS:( B ) ``` ```python 4. 下列程式建立的檔案物件,可以執行何種動作? f=open('file1.txt','w') f.write("Hello Python!") f.close() (A) 讀取 (B) 寫入 (C) 可讀取也可寫入 (D) 以上皆非 ``` ```python ANS:( B ) ``` ```python 5. 執行下列程式,下列顯示結果何者正確? try: print(x) except: print("y") finally: print("z") (A) x (B) y (C) xz (D) yz ``` ```python try: print(x) except: print("y") finally: print("z") ``` y z ```python ANS:( D ) ``` ```python 6. open(filename,mode,encode) 函式的參數中,其中只有那一個參數是必填? (A) filename (B) mode (C) encode (D) 以上皆是 ``` ```python ANS:( A ) ``` ```python 7. 如果作業系統是繁體中文 Windows 系統,預設的編碼為何? (A) UTF-8 (B) cp950 (C) unicode (D) GB2312 ``` ```python ANS:( B ) ``` ```python 8. 下列有關 readlines() 的敘述,何者正確? (A) 會讀取全部文件內容 (B) 以串列方式傳回 (C) 包括「\n」跳列字元,甚至是隱含的字元 (D) 以上皆是 ``` ```python ANS:( D ) ``` ```python 9. 執行下列程式,下列顯示結果何者正確? n=1 try: print(n) except: print("變數不存在!") (A) 1 (B) n (C) 變數不存在 (D) 以上皆是 ``` ```python n=1 try: print(n) except: print("變數不存在!") ``` 1 ```python ANS:( A ) ``` ```python 10. 班上的除錯高手大匹,在他的程式中加入了錯誤的補捉在 try…except…finally 敘述中,無論例外有沒有發生都會執行下列那些程式區塊? (A) try (B) except (C) finally (D) 以上皆是 ``` ```python ANS:( C ) ``` ### 【期末專題】 # 凱薩密碼 參考文件: 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) ``` 請輸入要加密的訊息: Firstly, each character of the initial text (message to encrypt) is converted in a number from 0 to 25, corresponding to its position in the Latin alphabet which contains 26 letters --> (a = 0, b = 1 ... z = 25 ). Then, each number obtained is transformed by an affine function (f(x) = 1x + b). "x" is representing the number while "b" is defined during the encryption. "b" is the key used to decrypt the final message. If we take all the images and put them in a list, we obtain n numbers corresponding to n characters of the initial text. The next step consists in finding the values of modulo 26 of each number. (Modulo means remainder) 請輸入密鑰: 4 加密結果: jmvwxp( diegldglevegxivdsjdxlidmrmxmepdxi.xdbqiwwekidxsdirgv(txcdmwdgsrzivxihdmrdedryqfivdjvsqddxsd dgsvviwtsrhmrkdxsdmxwdtswmxmsrdmrdxlidpexmrdeptlefixd,lmgldgsrxemrwddpixxivwddbedd dfdddaaad)dddcaddxlir diegldryqfivdsfxemrihdmwdxverwjsvqihdf(derdejjmridjyrgxmsrdbjb.cdd.ddfcad.dmwdvitviwirxmrkdxlidryqfivd,lmpidfdmwdhijmrihdhyvmrkdxlidirgv(txmsradfdmwdxlidoi(dywihdxsdhigv(txdxlidjmrepdqiwwekiaddmjd,idxeoideppdxlidmqekiwderhdtyxdxliqdmrdedpmwx d,idsfxemrdrdryqfivwdgsvviwtsrhmrkdxsdrdglevegxivwdsjdxlidmrmxmepdxi.xadxlidri.xdwxitdgsrwmwxwdmrdjmrhmrkdxlidzepyiwdsjdqshypsddsjdiegldryqfivadbqshypsdqierwdviqemrhivc 解密結果: firstly, each character of the initial text (message to encrypt) is converted in a number from to , corresponding to its position in the latin alphabet which contains letters (a , b ... z ). then, each number obtained is transformed by an affine function (f(x) x b). x is representing the number while b is defined during the encryption. b is the key used to decrypt the final message. if we take all the images and put them in a list, we obtain n numbers corresponding to n characters of the initial text. the next step consists in finding the values of modulo of each number. (modulo means remainder) ![png](output_46_1.png)